Methods of cleaning files and environment variables in Linux system

Time:2021-6-11

Use the find command to help you find the files that need to be cleaned
One problem that plagues almost all file systems — UNIX and others — is the accumulation of files. Few people are willing to spend time cleaning up the files they no longer use and reorganizing the file system. As a result, the files become very confusing and it is difficult to find useful things. It will be a lasting challenge to make them run well, maintain backup and easy to manage.

One of the solutions I’ve seen is to suggest that users create a summary report or “profile” of all the data fragments to report the number of all the files; The oldest, the latest, the largest file; And count who owns these files and other data. If someone had seen a folder five years ago that contained 500000 files, they might have deleted them – or at least archived and compressed them. The main problem is that too large a folder will make people worry about deleting something important by mistake. If there is a way to describe a folder to help show the nature of the file, then you can clean it up.

When I’m going to make a summary report on UNIX file system, several useful UNIX commands can provide some very useful statistics. To count the number of files in the directory, you can use such a find command.

Copy code

The code is as follows:

$ find . -type f | wc -l
187534

Although it is more complicated to find the oldest and newest files, it is quite convenient. In the following command, we use the find command to find the file again. The file is sorted by file time and displayed in year month day format. The one at the top of the list is obviously the oldest.

In the second command, we do the same, but print the last line, which is the latest.

Copy code

The code is as follows:

$ find -type f -printf ‘%T+ %p\n’ | sort | head -n 1
2006-02-03+02:40:33 ./skel/.xemacs/init.el
$ find -type f -printf ‘%T+ %p\n’ | sort | tail -n 1
2015-07-19+14:20:16 ./.bash_history

The printf command outputs% t (file date and time) and% P (file name with path) parameters.

If we look up the home directory, we will no doubt find that the history file (such as. Bash_ History) is up-to-date, which doesn’t help. You can ignore these files through “UN grepping”, or you can ignore files that start with. As shown in the figure below.

Copy code

The code is as follows:

$ find -type f -printf ‘%T+ %p\n’ | grep -v “\./\.” | sort | tail -n 1
2015-07-19+13:02:12 ./isPrime

To find the largest file, use the% s (size) parameter, including the file name (% f), because that’s what we want to show in the report.

Copy code

The code is as follows:

$ find -type f -printf ‘%s %f \n’ | sort -n | uniq | tail -1
20183040 project.org.tar

Owner of statistics file, using% U (owner)

Copy code

The code is as follows:

$ find -type f -printf ‘%u \n’ | grep -v “\./\.” | sort | uniq -c
180034 shs
7500 jdoe

If the file system can record the last access date, it will also be very useful to see if the file has been accessed, for example, within two years. This will give you a clear sense of the value of these documents. This last access (% a) parameter is used as follows:

Copy code

The code is as follows:

$ find -type f -printf ‘%a+ %p\n’ | sort | head -n 1
Fri Dec 15 03:00:30 2006+ ./statreport

Of course, if most of the recently accessed files are long ago, it seems that you need to deal with more files.

Copy code

The code is as follows:

$ find -type f -printf ‘%a+ %p\n’ | sort | tail -n 1
Wed Nov 26 03:00:27 2007+ ./my-notes

If you want to have a clear hierarchy, you can create a summary report for a file system or a large directory, showing the date range, the largest file, the file owners, the oldest file and the latest access time of these files, which can help the file owners to determine which folders are important and which should be cleaned up.

How to temporarily clear bash environment variables before running commands on Linux
How to display the current environment?
Open the terminal application and enter one of the following commands:

Copy code

The code is as follows:

printenv

or

Copy code

The code is as follows:

env

Output example:

Count the number of environment variables
Enter the following command:

Copy code

The code is as follows:

env | wc -l
Printenv | WC – L # or

Output example:

Copy code

The code is as follows:

20

Run the program in a clean bash / KSH / Zsh environment
The syntax is as follows:

Copy code

The code is as follows:

env -i your-program-name-here arg1 arg2 …

For example, to_ Proxy and / or any other environment variables. Temporarily clear all bash / KSH / Zsh environment variables and run WGet program:

Copy code

The code is as follows:

env -i /usr/local/bin/wget www.cyberciti.biz
env -i wget www.cyberciti.biz #Or

This is very useful when you want to run a command regardless of any environment variables that have been set. I use this command many times a day to ignore http_ Proxy and other environment variables I set.

Example: using HTTP_ proxy

Copy code

The code is as follows:

$ wget www.cyberciti.biz
–2015-08-03 23:20:23– http://www.cyberciti.biz/
Connecting to 10.12.249.194:3128… connected.
Proxy request sent, awaiting response… 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’
index.html [ <=> ] 36.17K 87.0KB/s in 0.4s
2015-08-03 23:20:24 (87.0 KB/s) – ‘index.html’ saved [37041]

Example: ignore http_ proxy

Copy code

The code is as follows:

$ env -i /usr/local/bin/wget www.cyberciti.biz
–2015-08-03 23:25:17– http://www.cyberciti.biz/
Resolving www.cyberciti.biz… 74.86.144.194
Connecting to www.cyberciti.biz|74.86.144.194|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.1’
index.html.1 [ <=> ] 36.17K 115KB/s in 0.3s
2015-08-03 23:25:18 (115 KB/s) – ‘index.html.1’ saved [37041]

-The I option causes the env command to completely ignore its inherited environment. However, it does not prevent your commands (such as WGet or curl) from setting new variables. At the same time, you should also pay attention to the side effects of running bash / KSH shell

Copy code

The code is as follows:

Env – I env | WC – L # empty##
#Now run bash##
env -i bash
##Bash sets a new environment variable##
env | wc -l

Example: setting an environment variable
The syntax is as follows:

Copy code

The code is as follows:

env var=value /path/to/command arg1 arg2 …
##Or##
var=value /path/to/command arg1 arg2 …

For example, set http_ proxy:

Copy code

The code is as follows:

env http_proxy=”http://USER:[email protected]:3128/” /usr/local/bin/wget www.cyberciti.biz