我想从应用程序中删除除最新版本之外的所有以前版本的Tableau Reader.app。我想通过命令行来做这件事。我遇到的问题是,除了文件tableau reader 2019.2
之外,我想删除所有包含tableau reader
的文件。
我尝试了多种方法,但都不起作用。我对此并不是很有经验。任何帮助都是非常感谢的。下面的脚本:检查应用程序是否正在运行,如果没有,就删除不同的版本(我需要帮助)
#!/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
发布于 2019-08-16 14:55:57
试一试
ls "tableau reader"* | sort | head -n -1 | xargs -d '\n' rm
发布于 2019-08-16 14:57:13
您可以尝试这样做:
find $HOME/Applications -type f -not -name 'Tableau Reader 2019.2.app' | grep "Tableau Reader" | xargs -I {} rm {}
脚本将查找除Tableau Reader 2019.2.app
以外的所有文件,并使用grep
将包含Tableau Reader
的所有文件分开,然后将其删除。
发布于 2019-08-16 16:27:42
这只能通过find
命令实现,但在它删除某些内容之前,让我们对其进行测试:
find "$HOME/Applications/" -maxdepth 1 -type f -name 'Tableau Reader*.app' -not -name 'Tableau Reader 2019.2.app'
查看它是否只返回要删除的文件的列表:
文件列表应如下所示,其中包含您的主目录而不是/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
让我们看看用于驱动find
的选项
查找用于启动目录的命令
find
:files"$HOME/Applications/"
:search-maxdepth 1
:留在目录中,请勿进入sub-directories-type f
:find regular files-name 'Tableau Reader*.app'
:查找名称与此pattern-not -name 'Tableau Reader 2019.2.app'
匹配但与其他模式不匹配的文件现在,如果您对该列表感到满意并确定要删除这些文件;如果您像这样添加-delete
选项,find
可以为您完成此操作:
find "$HOME/Applications/" -maxdepth 1 -type f -name 'Tableau Reader*.app' -not -name 'Tableau Reader 2019.2.app' -delete
https://stackoverflow.com/questions/57526579
复制相似问题