Remove path from find command output
Solution 1:
You can use the -printf
command line option with %f
to print just the filename without any directory information
find . -type f -mtime -14 -printf '%f\n' > deploy.txt
or you can use sed to just remove the ./
find . -type f -mtime -14 | sed 's|^./||' >deploy.txt
Solution 2:
The ./
should be harmless. Most programs will treat /foo/bar
and /foo/./bar
as equivalent. I realize it doesn't look very nice, but based on what you've posted, I see no reason why it would cause your script to fail.
If you really want to strip it off, sed
is probably the cleanest way:
find . -type d -mtime 14 | sed -e 's,^\./,,' > deploy.txt
If you're on a system with GNU find (e.g. most Linux systems), you can do it in one shot with find -printf
:
find . -type d -mtime 14 -printf "%P\n" > deploy.txt
The %P
returns the complete path of each file found, minus the path specified on the command line, up to and including the first slash. This will preserve any subdirectories in your directory structure.
Solution 3:
Why do you need to strip off the ./
? It is a valid to have in a path. So
cp -i dir1/./somefile dir2/./somefile
is just ok!
But if You would like to strip off the directory name in find You can use the %P
arg to -printf
.
man find(1) says:
%P File's name with the name of the command line argument under which it was found removed.
An example
$ find other -maxdepth 1
other
other/CVS
other/bin
other/lib
other/doc
other/gdbinit
$ find other -maxdepth 1 -printf "%P\n"
CVS
bin
lib
doc
gdbinit
Mind the first empty line! If You want to avoid it use -mindepth 1
$ find other -mindepth 1 -maxdepth 1 -printf "%P\n"
CVS
bin
lib
doc
gdbinit
Solution 4:
"find -printf" solution won't work on FreeBSD because find don't have such an option. In this case AWK can help. It returns a last name ($NF), so it can work on any depth.
find /usr/local/etc/rc.d -type f | awk -F/ '{print $NF}'
PS: taken from D.Tansley "Linux and Unix shell programming" book
Solution 5:
Well you have a few options. You could use the -printf
option in find to only print out the filename, or you could use a tool like sed to simply strip out the ./
.
# simply print out the filename, will break if you have sub-directories.
find . -mtime -14 -printf '%f\n'
# strip a leading ./
find . -mtime -14 | sed -e 's/^\.\///'