简体   繁体   English

如何删除Linux代码文件中未使用的文件?

[英]How can I delete files that are not used in code files in linux?

I am running Fedora 18 linux and I have a PHP project that I have been working on for some time. 我正在运行Fedora 18 linux,并且已经有一段时间从事PHP项目了。 I am trying to clean things up for a production deploy of a web application. 我正在尝试为Web应用程序的生产部署清理问题。 I have a folder of icon images that over time has collected files that are not used in my code any more, either because I changed to a different icon in code, or the image file was used to create other icons. 我有一个图标图像文件夹,随着时间的推移,该文件夹已收集了我的代码中不再使用的文件,这是因为我更改为代码中的其他图标,或者该图像文件用于创建其他图标。 What I am looking to do is to make a backup copy of the entire code project, and HOPEFULLY using a combination of find , rm and grep on the command line, scan the entire folder of the images, and if the images are not used anywhere in my code files, delete them. 我要做的是制作整个代码项目的备份副本,并在命令行上结合使用findrmgrep进行HOPEFULLY,扫描图像的整个文件夹,如果图像未在任何地方使用在我的代码文件中,将其删除。 I did some searching on the web and I am finding things that find a line of text in a file and delete it, but I have not found anything quite like what I am trying to do. 我在网上进行了一些搜索,发现在文件中找到一行文本并将其删除的内容,但是却没有找到与我尝试执行的内容完全相同的内容。

Any help is appreciated... 任何帮助表示赞赏...

So here is what I came up with. 这就是我的想法。 I put together a shell script that does what I need. 我整理了一个可以满足我需求的shell脚本。 For the benefit of those who stumble upon this, and for those who want to critique my solution, here it is. 对于那些偶然发现此问题的人以及对我的解决方案提出批评的人,这里就是。 I chose to skip files that were found in .xcf files because these are only used to create many of the icon files and some of the .png images would grep to these .xcf files. 我选择跳过在.xcf文件中找到的文件,因为这些文件仅用于创建许多图标文件,而某些.png图像会复制到这些.xcf文件中。

#!/bin/bash
FILES=/var/www/html/support_desk/templates/default/images/icons/*
codedir=/var/www/html/support_desk_branch/

for f in $FILES
do
  bn=$(basename $f)

  ext="${bn##*.}"

  echo "Processing $bn file..."

  if ! fgrep --quiet -R $bn $codedir; then
    if [ ext != 'xcf' ]; then
      rm $f
    fi
  fi

done

Now I have ONLY the image files that are used in the PHP script files. 现在,只有PHP脚本文件中使用的图像文件。 Just so as not to miss any of the icon files used in the menu, which is defined in a table in a mysql database, I created an sql dump file of the data for that table, and put it in the path of the application files prior to running the shell script. 为了不丢失在mysql数据库的表中定义的菜单中使用的任何图标文件,我创建了该表数据的sql转储文件,并将其放在应用程序文件的路径中在运行shell脚本之前。

The simplest way to find unused icon files would be to do a build of your complete project and then look at the access-times of the icon-files. 查找未使用的图标文件的最简单方法是构建整个项目,然后查看图标文件的访问时间。 Those that were not read recently (including with grep, of course) would show up readily. 那些最近没有读过的内容(当然包括grep在内)将很容易显示出来。

For instance, supposing that you did a backup an hour ago, and did a build ten minutes ago — the access times would be distinct. 例如,假设您在一个小时前进行了备份,而在十分钟前进行了构建,则访问时间会有所不同。 Then 然后

find . -amin +15 -type f

should give a nice list of "unused" files. 应该给出一个很好的“未使用”文件列表。 If you're sure of the list (you did do a backup, right?) then you could purge the unused files: 如果您确定该列表(确实做了备份,对吗?),则可以清除未使用的文件:

find . -amin +15 -type f -exec rm -i {} \;

If you are really certain, you can remove the -i option. 如果确实可以确定,则可以删除-i选项。

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

相关问题 Linux中可用于删除文件的系统调用 - System calls in Linux that can be used to delete files 如何从Linux中的shell中删除以._开头的所有文件? - How can I delete all files starting with ._ from the shell in Linux? 如何在没有find的情况下在linux shell脚本中根据日期查找和删除文件? - How can I find and delete files based on date in a linux shell script without find? 如何为IP摄像头网络摄像头自动在Linux中重命名,复制和删除文件? - How can I automatically rename, copy and delete files in linux for my ip camera webcam? 如何删除旧的 Gradle 文件和 IntelliJ 在 Linux 上下载的 JDK? - How can I delete old Gradle files and JDKs downloaded by IntelliJ on Linux? 如何在Linux中显示.xed文件? - How can I display .xed files in Linux? 我如何在Linux中找到可疑文件 - how can i find suspicious files in linux 存在其他文件时,如何在Linux中解压缩文件 - How can I unzip files in Linux when other files are present 我想删除linux中的所有文件和目录 - I wanna to delete all files and directories in linux 如何使用我的php代码访问Linux服务器上具有不同用户权限的文件夹的文件? - How can I access the files of folder of different user permissions on linux server with my php code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM