简体   繁体   English

删除名称相似的文件,除了一个?

[英]Remove files with the similar name except one?

该图显示了TR的不同版本-我要删除,但不删除任何其他应用程序并保留TR 2091.2的最新版本

I want to remove all previous versions of Tableau Reader.app from applications apart from the latest one. 我想从最新的应用程序中删除Tableau Reader.app的所有先前版本。 I want to do this through command line. 我想通过命令行执行此操作。 The problem that I am having is I want to remove all files containing tableau reader except for the file tableau reader 2019.2 . 我遇到的问题是我想删除所有包含tableau reader的文件,但文件tableau reader 2019.2

I have tried multiple ways but they aren't working. 我尝试了多种方法,但是它们没有用。 I am not very experienced with this. 我对此不是很有经验。 Any help is appreciated. 任何帮助表示赞赏。 The script below: Checks if the Application is running and if not goes about deleting the different versions(bit I need help on) 下面的脚本:检查应用程序是否正在运行,如果不运行,则删除不同版本(我需要帮助的位)

#!/bin/bash
shopt -s extglob

process="$4"
processrunning=$( ps axc | grep "${process}" )

if [ "$processrunning" != "" ] ; then

echo "$process IS running, do nothing" 
echo "error: script failed"

exit 1
else 

echo "$process is not running and will remove the old versions"

find $HOME/Applications/ -type f -not -name 'Tableau Reader 2019.2.app' | grep "tableau reader" | xargs rm

#rm -r $HOME/Applications/Tableau Reader?*.app![$HOME/Applications/"Tableau Reader 2019.2.app"]

#find. -type f !( -name 'Tableau Reader 2019.2.app') -exec rm -f "Tableau Reader"*.app {} +

fi

尝试

ls "tableau reader"* | sort | head -n -1 | xargs -d '\n' rm

You can try this: 您可以尝试以下方法:

find $HOME/Applications -type f -not -name 'Tableau Reader 2019.2.app' | grep "Tableau Reader" | xargs -I {} rm {}

The scripts finds all the files except with the name Tableau Reader 2019.2.app and from that using grep all files containing Tableau Reader are separated and then deletes it. 脚本查找所有文件,但名称为Tableau Reader 2019.2.app ,使用grep从中查找所有包含Tableau Reader文件,然后将其删除。

This is achievable with the find command only, but lets test it before it deletes something: 这只能通过find命令实现,但是在删除某些内容之前先对其进行测试:

find "$HOME/Applications/" -maxdepth 1 -type f -name 'Tableau Reader*.app' -not -name 'Tableau Reader 2019.2.app'

See if it returns the list of the files you want to delete only: 查看它是否仅返回您要删除的文件列表:

The list of files should look like this, with your home directory instead of /tmp : 文件列表应如下所示,使用您的主目录而不是/tmp

/tmp/Applications/Tableau Reader 10.5.app
/tmp/Applications/Tableau Reader 2012.app
/tmp/Applications/Tableau Reader 10.3.app
/tmp/Applications/Tableau Reader 1.app

Lets see the options used to drive find : 让我们来看看用于驱动find的选项:

  • find : the command to find files find :查找文件的命令
  • "$HOME/Applications/" : the directory to start the search "$HOME/Applications/" :开始搜索的目录
  • -maxdepth 1 : stay in the directory, do not go into sub-directories -maxdepth 1 :保留在目录中,不要进入子目录
  • -type f : find regular files -type f :查找常规文件
  • -name 'Tableau Reader*.app' : find files with names matching this pattern -name 'Tableau Reader*.app' :查找名称与此模式匹配的文件
  • -not -name 'Tableau Reader 2019.2.app' and not matching this other pattern -not -name 'Tableau Reader 2019.2.app'并且与其他模式不匹配

Now if you are satisfied with the list and are sure that you want to delete these files; 现在,如果您对列表满意,并确定要删除这些文件; find can do it for you if you add it the -delete option like this: 如果您添加-delete选项,则find可以为您完成此操作:

find "$HOME/Applications/" -maxdepth 1 -type f -name 'Tableau Reader*.app' -not -name 'Tableau Reader 2019.2.app' -delete

How about something like 怎么样

rm -f $(find . -name "tableau reader*" -type f -exec ls -t {} \\; | head -n +2)

Essentialy, run a subshell (as can be seen between $( and ) to obtain what to remove. Within that subshell, use find in the current directory only ( . ) with result type of file ( f ) exec an ls in time modified order using the -t flag. Then pipe to head starting at the 2nd result ( -n +2 ) (which ensures your latest file is excluded from the rm command. 本质上,运行一个子shell(可以在$()之间获取要删除的内容。在该子shell中,仅使用当前目录中的find. )的结果类型为文件( f )exec并按时间顺序修改ls使用-t标志。然后管head开始于第二结果( -n +2 )(其确保您的最新的文件被从排除rm命令。

Note - you have to be careful with ls though as you'll get strange results if filenames contain some special characters, but for your use-case it looks like it should be fine. 注意-尽管必须小心ls因为如果文件名包含一些特殊字符,将会得到奇怪的结果,但是对于您的用例来说,看起来应该没问题。

What I do with these tasks is copy some test files to a new directory to get my expression perfect before scripting it. 我要执行的这些任务是在脚本编写脚本之前将一些测试文件复制到新目录中,以使表达式更加完美。 Make sure you use cp -a to preserve the file dates though. 但是请确保使用cp -a保留文件日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM