Shorthand for specifying path as pwd in Unix? - unix

I'd like to know if there's a built in shortcut or a way to create an alias for the path in a command when the path is the pwd. For example, lets say my pwd is ~/Desktop/Unix_Folder/Unix_Sub_Folder and I wanted do something like ...
find ~/Desktop/Unix_Folder/Unix_Sub_Folder -name '*txt'.
I'm thinking there must be a more efficient way to reference the pwd without typing it out, but I don't know what it is. Maybe there isn't, but it would be nice to know.
Thanks,
~Benny

How about simply:
find . -name '*txt'
(I hope I haven't misunderstood the question.)

Using this command also we can do :
find `pwd` -iname "*.txt" -print
pwd - will print the current directory

Related

Is it possible remove the root directory from find command output?

I want to move some files around and thought that find would be a good option to select the correct files. So I look for the files:
find somedir -iname "somefile"
somedir/subdir1/subdir2/somefile
somedir/subdir2/somefile
somedir/subdir3/somefile
somedir/subdir4/somefile
somedir/subdir5/somefile
Thats not very helpful for what I'm planning next. What I need would be:
find somedir -iname "somefile" -magic-option
subdir1/subdir2/somefile
subdir2/somefile
subdir3/somefile
subdir4/somefile
subdir5/somefile
What would -magic-option be?
Obviously a simple printout is not what I had in mind. The final command will also have a '-exec'. Something like:
find somedir -iname "somefile" -magic-option -exec some_command 'somedir/{}' 'someotherdir/{}' ';'
I'm surprised I couldn't find anything as removing the root directory from the result seem a pretty obvious feature.
If the answer to the question is 'NO' then that's ok. I have a crude plan B using pushd and for loops. But find would be more elegant.
It is non-standard, but with gnu find (4.6.0.225-235f), you could do:
find somedir -iname somefile -printf %P\\n
From the documentation:
%P File's name with the name of the starting-point under which it was found removed.
If you want a generic solution, it seems simple enough to filter the output with something like:
find somedir -iname somefile | sed 's#^[^/]*/##'
Both of those solutions will fail horribly if any of your filenames contain a newline, so if you want a robust solution you would want to do something like:
find somedir -iname somefile -printf %P\\0
Looks like you need the mindepth flag.
find somedir -mindepth 2 -iname "somefile"
This will ignore the directory you are in and search from one level down recursively.

find command in unix withe regex

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.

"find" command returning nothing when searching through absolute path

Thought there might be a simple solution to this, but I can't seem to find it anywhere. It's a simple-enough problem. Say I have the following folder/file structure:
/home/
text1.txt
/mydir/
text2.txt
Then I input the command:
find . -name *.txt
This command returns "text1.txt" when called from within /home, and returns "text2.txt" when called from within /home/mydir, as it should.
However, when calling the following from /home...:
find /home/mydir -name *.txt
it returns nothing. My expectation is that it would return "text2.txt." Any thoughts? I have already checked to see if I have any wayward aliases assigned to find, and I have nothing.
It is also worth it to note that I have two unix machines. The use of an absolute path for "find" works on one machine and not the other. Can't go into much more detail than that, I'm afraid. Just looking for a direction to investigate this more.
Thanks to anyone who can help :-)
You should use
find . -name "*.txt"
otherwise bash will extract *.txt to text1.txt resulting in the following command:
find . -name text1.txt
And it will no longer match text2.txt

Find command to find Zip files with ignorecase

Can some one tell me how to use Find command to find files of extension Zip,ZIP,zip. ?
find . -iname *.zip is not working for me in AIX.
You need to uses quotes around the pattern matching part. So
find . -iname '*.zip'
will do fine.
assuming that your shell supports character classes as part of its wild-card processing, most do, try
find . -name '*.[Zz][Ii][Pp]'
-iname ? I don't know that one.
Yes, sorry, you need a starting directory, in this case the '.' that you have flagged as missing.
I hope this helps.

Using mtime other than with FIND

I am trying to write a script which will move files older than 1 day to an archive directory. I used the following find command:
for filename in `find /file_path/*.* -type f -mtime +1`
This fails since my argument list is too big to be handled by find. I got the following error:
/usr/bin/find: arg list too long
Is it possible to use find in an IF-ELSE statement? Can someone provide some examples of using mtime other then in find.
Edit: To add the for loop of which the find is a part.
find /file_path -name '*.*' -mtime +1 -type f |
while read filename
do ...move operation...
done
That assumes your original code was acceptable in the way it handled spaces etc in file names,
and that there is no sensible way to do the move in the action of find. It also avoids problems with overlong argument lists.
Why not just use the -exec part of find?
If you just want to cp files, you could use
find /file_path -name "." -mtime +1 -type f | xargs -i mv {} /usr/local/archived

Resources