I want to create an alias that will run find for the string I pass and surround it with wildcards. The alias below will add the first wildcard, but not the second. The same behavior occurs if I try using \!:1 to only use the first argument as well. Is this an issue with \!* being followed by * or something else?
alias ff 'find . -type f -name "*\!**"'
which ff
ff: aliased to find . -type f -name "*!**"
if I have a file foobar then:
ff oo returns nothing
ff oo* returns foobar
Related
I'm trying to automate my test runner better. For that I need the update file name in a variable. As this name depends on a the version I'm trying to use a find with a pattern to get the file name. That works just fine in bash.
However if I use that same pattern in expect find complains that it can't find anything.
My guess is that expect is doing something to my wildcards. However my experiments with {}, "", '' or \ didn't result in it working.
I guess I could create a helper sh script to write it into a file and then read that file but I don't like that solution and there has to be an option to pass characters with special tcl meaning as arguments.
At the moment my call looks something like this with an absolute path in front of the pattern:
set pattern {[0-9]*/*test*}
set updateFile [exec find ${pattern} -type f]
The result is that find reports '[0-9]*/*test*': No such file or directory. The pattern is what I would expect and when I call find [0-9]*/*test* -type f in bash it results in the expected file path. Find also works fine as long as I don't have any wild cards.
Has anybody an idea what is wrong?
When you run find [0-9]*/*test* -type f in Bash, it's Bash who interprets the wildcard [0-9]*/*test* and expand it to multiple files. And then Bash would pass the expanded multiple files to find. That's to say find never sees the wildcard.
For exec find $pattern -type f, Tcl would not interpret what's in $pattern and pass it directly to find. Unfortunately find also does not interpret the wildcards here so it fails with error like find : '[0-9]*/*test*': No such file or directory.
To work around, you can invoke find with bash -c:
exec bash -c "find $pattern -type f"
Can someone tell me what this command does : find ./ -regex ".*"\!*"*" ?
Guess based on too little information in the question:
This may be part of the definition of an alias like
alias f 'find ./ -regex ".*"\!*"*"'
for csh or tcsh which could be called like
f some pattern
to recursively find files that somehow match the specified pattern.
I've an issue with file name pattern to be provided in order to fetch the pattern file from the FTP server.
Currently, I am using ABC_YYYYMMDD*.sha1 as the pattern to fetch files. It fetches the last file using the above pattern. Sometimes, .gz.sha1 comes later and sometimes, the other one.
ABC_20160801060000.sha1
ABC_20160801060000.txt.gz.sha1
I would need to provide the file name pattern in such a way that the file should always pick ABC_YYYYMMDDHHMISS.sha1 only.
Need a fool proof pattern which matches the required one only?
You can use find -regex option for matching these file-names:-
find . -type f -regextype posix-extended -regex '.*ABC_20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])([0-2][0-3])([0-5][0-9])([0-5][0-9])\.sha1'
Am using the -regex flag supported by find for this over the -name flag which does simple glob pattern matching.
The man page of find says below for the -regex:-
-regex pattern
File name matches regular expression pattern. This is a match
on the whole path, not a search.
-regextype name
This option controls the variety of regular expression syntax
understood by the ‘-regex’ and ‘-iregex’ tests. This option is
positional; that is, it only affects regular expressions which
occur later in the command line. If this option is not given, GNU
Emacs regular expressions are assumed.
More about posix-extended regex type at this page. Other supported regex-types can be found here.
To see it in action:-
$ ls ABC_2016*
ABC_20161231225950.sha1 ABC_20169231225990.sha1
$ find . -type f -regextype posix-extended -regex '.*ABC_20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])([0-2][0-3])([0-5][0-9])([0-5][0-9])\.sha1'
./ABC_20161231225950.sha1
Update:-
If the regextype is not supported in the find version, a simple glob construct using the -name flag can be used to achieve the same.
$ ls ABC_2016*
ABC_20161231225950.sha1 ABC_20169231225990.sha1
$ find . -type f -name 'ABC_2[0-9][0-9][0-9][0-1][0-2][0-3][0-9][0-2][0-2][0-5][0-9][0-5][0-9].sha1'
./ABC_20161231225950.sha1
Is there a command which lists all folders that matches a wildcard expression? Example, if there are thousands of directories and I only want those ending in M or those starting in JO to be listed, can I do that with a certain Linux command? Thanks!
Use find command, for example:
# find anything that start with 'jo' end with 'm' (case insensitive)
find . -iname 'jo*m'
You can execute any command after that, for example:
# find just like above but case sensitive, and move them to `/tmp`
find . -name 'JO*M' -exec mv -v {} /tmp \;
To find only a directory, you can use -type d flag, for example:
# find any directory that start with JO
find . -name 'JO*' -type d
Explanation, first argument is the starting directory, . means current directory. The next argument means the search criteria -name for case sensitive search, -iname for case insensitive search, -type for type of item search, -exec to execute certain command where the {} is the file name matched. You can learn more here or for your specific case here.
Im a beginner scripter, writing scripts in tcsh and csh(these are teached in my course)
Im writing a script which uses find for putting path of directories
this is the part of the script:
set list = (`find $PATH -type d`)
it works fine until the file or directory names arent named such as:
#fi##lename&& or −filename or :−,?!drectoryanem!-``
These special characters i couldnt handle i changed the find script to:
set list = ("`find $PATH -type d`")
bit none of these works, when i want to use the path from the list in this next script:
foreach i ($list:q)
foreach file (`find "$i" -maxdepth 1 -type f`)
....
end
end
it couldnt handle these special file names, so i get many errors like find: −."!filename: no such filename or directory
I worked it out
It had to be this way:
set subor = ("`find "'"$i"'" -type f -maxdepth 1`")
now it ignores everything in the filenames
and in:
foreach j ($subor:q)
i quoted it this way it ignores the white characters in file names