I'm using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get ./dir1/dir2/file.txt
and I want to get file.txt
我将find用于目录中的所有文件,因此我得到一个路径列表。但是,我只需要文件名。即我。/ dir1 / dir2 /文件。txt和我想要文件。txt
6 个解决方案
#1
236
In GNU find
you can use -printf
parameter for that, e.g.:
在GNU中,你可以使用-printf参数,例如:
find /dir1 -type f -printf "%f\n"
#2
113
If your find doesn't have a -printf option you can also use basename:
如果你的发现没有-printf选项,你也可以使用basename:
find ./dir1 -type f -exec basename {} \;
#3
20
If you are using GNU find
如果您正在使用GNU查找
find . -type f -printf "%f\n"
Or you can use a programming language such as Ruby(1.9+)
也可以使用Ruby(1.9+)之类的编程语言
$ ruby -e 'Dir["**/*"].each{|x| puts File.basename(x)}'
If you fancy a bash (at least 4) solution
如果您想要一个bash(至少4个)解决方案
shopt -s globstar
for file in **; do echo ${file##*/}; done
#4
14
Use -execdir
which automatically holds the current file in {}
, for example:
使用-execdir,它自动将当前文件保存在{}中,例如:
find . -type f -execdir echo '{}' ';'
You can also use $PWD
instead of .
(on some systems it won't produce extra dot in the front).
您也可以使用$PWD而不是。(在某些系统中,它不会在前面产生额外的点)。
-execdir utility [argument ...] ;
-execdir公用事业[论点…];
The
-execdir
primary is identical to the-exec
primary with the exception that utility will be executed from the directory that holds the current file.-execdir主目录与-exec主目录相同,但该实用程序将从保存当前文件的目录中执行。
When used +
instead of ;
, then {}
is replaced with as many pathnames as possible for each invocation of utility. In other words, it'll print all filenames in one line.
当使用+而不是;时,则在每次调用实用程序时,用尽可能多的路径名替换{}。换句话说,它将在一行中打印所有文件名。
#5
10
If you want to run some action against the filename only, using basename
can be tough.
如果您想只对文件名运行一些操作,那么使用basename可能比较困难。
For example this:
例如:
find ~/clang+llvm-3.3/bin/ -type f -exec echo basename {} \;
will just echo basename /my/found/path
. Not what we want if we want to execute on the filename.
将仅仅回显basename /my/found/path。如果我们想要在文件名上执行,这不是我们想要的。
But you can then xargs
the output. for example to kill the files in a dir based on names in another dir:
但是您可以对输出进行xargs处理。例如,根据另一个目录中的名称杀死目录中的文件:
cd dirIwantToRMin;
find ~/clang+llvm-3.3/bin/ -type f -exec basename {} \; | xargs rm
#6
-4
I've found a solution (on makandracards page), that gives just the newest file name:
我找到了一个解决方案(在makandracards页面),它只提供了最新的文件名:
ls -1tr * | tail -1
(thanks goes to Arne Hartherz)
(感谢Arne Hartherz)
I used it for cp
:
我用它来做cp:
cp $(ls -1tr * | tail -1) /tmp/
#1
236
In GNU find
you can use -printf
parameter for that, e.g.:
在GNU中,你可以使用-printf参数,例如:
find /dir1 -type f -printf "%f\n"
#2
113
If your find doesn't have a -printf option you can also use basename:
如果你的发现没有-printf选项,你也可以使用basename:
find ./dir1 -type f -exec basename {} \;
#3
20
If you are using GNU find
如果您正在使用GNU查找
find . -type f -printf "%f\n"
Or you can use a programming language such as Ruby(1.9+)
也可以使用Ruby(1.9+)之类的编程语言
$ ruby -e 'Dir["**/*"].each{|x| puts File.basename(x)}'
If you fancy a bash (at least 4) solution
如果您想要一个bash(至少4个)解决方案
shopt -s globstar
for file in **; do echo ${file##*/}; done
#4
14
Use -execdir
which automatically holds the current file in {}
, for example:
使用-execdir,它自动将当前文件保存在{}中,例如:
find . -type f -execdir echo '{}' ';'
You can also use $PWD
instead of .
(on some systems it won't produce extra dot in the front).
您也可以使用$PWD而不是。(在某些系统中,它不会在前面产生额外的点)。
-execdir utility [argument ...] ;
-execdir公用事业[论点…];
The
-execdir
primary is identical to the-exec
primary with the exception that utility will be executed from the directory that holds the current file.-execdir主目录与-exec主目录相同,但该实用程序将从保存当前文件的目录中执行。
When used +
instead of ;
, then {}
is replaced with as many pathnames as possible for each invocation of utility. In other words, it'll print all filenames in one line.
当使用+而不是;时,则在每次调用实用程序时,用尽可能多的路径名替换{}。换句话说,它将在一行中打印所有文件名。
#5
10
If you want to run some action against the filename only, using basename
can be tough.
如果您想只对文件名运行一些操作,那么使用basename可能比较困难。
For example this:
例如:
find ~/clang+llvm-3.3/bin/ -type f -exec echo basename {} \;
will just echo basename /my/found/path
. Not what we want if we want to execute on the filename.
将仅仅回显basename /my/found/path。如果我们想要在文件名上执行,这不是我们想要的。
But you can then xargs
the output. for example to kill the files in a dir based on names in another dir:
但是您可以对输出进行xargs处理。例如,根据另一个目录中的名称杀死目录中的文件:
cd dirIwantToRMin;
find ~/clang+llvm-3.3/bin/ -type f -exec basename {} \; | xargs rm
#6
-4
I've found a solution (on makandracards page), that gives just the newest file name:
我找到了一个解决方案(在makandracards页面),它只提供了最新的文件名:
ls -1tr * | tail -1
(thanks goes to Arne Hartherz)
(感谢Arne Hartherz)
I used it for cp
:
我用它来做cp:
cp $(ls -1tr * | tail -1) /tmp/