[英]How can I list all unique file names without their extensions in bash?
I have a task where I need to move a bunch of files from one directory to another. 我有一项任务需要将一堆文件从一个目录移动到另一个目录。 I need move all files with the same file name (ie blah.pdf, blah.txt, blah.html, etc...) at the same time, and I can move a set of these every four minutes. 我需要同时移动具有相同文件名(即blah.pdf,blah.txt,blah.html等)的所有文件,并且每四分钟可以移动一组文件。 I had a short bash script to just move a single file at a time at these intervals, but the new name requirement is throwing me off. 我有一个简短的bash脚本,可以按这些间隔一次仅移动一个文件,但是新的名称要求使我不满意。
My old script is: 我的旧脚本是:
find ./ -maxdepth 1 -type f | while read line; do mv "$line" ~/target_dir/; echo "$line"; sleep 240; done
For the new script, I basically just need to replace find ./ -maxdepth 1 -type f
with a list of unique file names without their extensions. 对于新脚本,我基本上只需要用一个没有扩展名的唯一文件名列表替换find ./ -maxdepth 1 -type f
。 I can then just replace do mv "$line" ~/target_dir/;
然后,我可以只替换do mv "$line" ~/target_dir/;
with do mv "$line*" ~/target_dir/;
用do mv "$line*" ~/target_dir/;
. 。
So, with all of that said. 所以,说了这么多。 What's a good way to get a unique list of files without their file names with bash script? 用bash脚本获取不带文件名的唯一文件列表的好方法是什么? I was thinking about using a regex to grab file names and then throwing them in a hash to get uniqueness, but I'm hoping there's an easier/better/quicker way. 我当时正在考虑使用正则表达式来获取文件名,然后将其放入哈希中以获取唯一性,但是我希望有一种更容易/更好/更快的方法。 Ideas? 想法?
A weird-named files tolerant one-liner could be: 可以容忍一线的怪异文件可能是:
find . -maxdepth 1 -type f -and -iname 'blah*' -print0 | xargs -0 -I {} mv {} ~/target/dir
If the files can start with multiple prefixes, you can use logic operators in find. 如果文件可以以多个前缀开头,则可以在find中使用逻辑运算符。 For example, to move blah.* and foo.*, use: 例如,要移动blah。*和foo。*,请使用:
find . -maxdepth 1 -type f -and \( -iname 'blah.*' -or -iname 'foo.*' \) -print0 | xargs -0 -I {} mv {} ~/target/dir
Updated after comment. 评论后更新。
Here's how I'd do it: 这是我的处理方式:
find ./ -type f -printf '%f\n' | sed 's/\..*//' | sort | uniq | ( while read filename ; do find . -type f -iname "$filename"'*' -exec mv {} /dest/dir \; ; sleep 240; done )
Perhaps it needs some explaination: 也许需要一些解释:
find ./ -type f -printf '%f\\n':
find all files and print just their name, followed by a newline. find ./ -type f -printf '%f\\n':
查找所有文件并仅打印其名称,后跟换行符。 If you don't want to look in subdirectories, this can be substituted by a simple ls
; 如果您不想查看子目录,可以用一个简单的ls
代替。 sed 's/\\..*//'
: strip the file extension by removing everything after the first dot. sed 's/\\..*//'
:通过删除第一个点后的所有内容来去除文件扩展名。 Both foo.tar
ad foo.tar.gz
are transformed into foo
; foo.tar
和foo.tar.gz
都转换为foo
; sort | unique
sort | unique
: sort the filenames just found and remove duplicates; sort | unique
:对刚找到的文件名进行排序并删除重复项; (
: open a subshell: (
:打开一个子shell:
while read filename
: read a line and put it into the $filename
variable; while read filename
:读取一行并将其放入$filename
变量; find . -type f -iname "$filename"'*' -exec mv {} /dest/dir \\;
: find in the current directory ( find .
) all the files ( -type f
) whose name starts with the value in filename
( -iname "$filename"'*'
, this works also for files containing whitespaces in their name) and execute the mv
command on each one ( -exec mv {} /dest/dir \\;
) :在当前目录中find .
( find .
)名称以filename
值开头的所有文件( -type f
)(- -iname "$filename"'*'
,这也适用于名称中包含空格的文件)并执行每个命令上的mv
命令( -exec mv {} /dest/dir \\;
) sleep 240
: sleep sleep 240
:睡觉 )
: end of subshell. )
:subshell的结尾。 Add -maxdepth 1
as argument to find
as you see fit for your requirements. 添加-maxdepth 1
作为自变量,以find
符合您要求的内容。
Nevermind, I'm dumb. 没关系,我很傻。 there's a uniq command. 有一个uniq命令。 Duh. 咄。 New working script is: 新的工作脚本是:
find ./ -maxdepth 1 -type f | sed -e 's/.[a-zA-Z]*$//' | uniq | while read line; do mv "$line*" ~/target_dir/; echo "$line"; sleep 240; done
EDIT: Forgot close tag on code and a backslash. 编辑:忘记代码和反斜杠上的关闭标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.