Methods of cleaning files and cleaning environment variables in Linux systems

Source: Internet
Author: User
Tags empty http request printf sort

Use the Find command to help you locate files that need to be cleaned up.

A problem that almost haunts all file systems-including Unix and others-is the accumulation of files. Few people are willing to take the time to clean up files and file systems that they no longer use, and as a result, the files become confusing and difficult to find useful things to run well, maintain backups, and manage easily, which will be a lasting challenge.

One way I've seen a problem is to suggest that users create a summary report or "profile" of all the pieces of data, such as the number of files, the oldest, newest, largest files, and who owns the files. If someone sees a folder containing 500,000 files five years ago, they might be able to delete which files--or at least archive and compress them. The main problem is that too large a folder can cause people to worry about accidentally deleting something important. If you have a way to describe a folder to help show the nature of the file, then you can clean it up.

When I am ready to make a summary report of the Unix file system, several useful UNIX commands can provide some very useful statistical information. To calculate the number of files in the directory, you can use such a Find command.

The code is as follows:

$ find. -type F | Wc-l

187534

Although finding the oldest and newest files is more complicated, it is quite convenient. In the following command, we use the Find command to locate the file again, sorted by file time and displayed in the format of year-month-day, 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 up to date.

The code is as follows:

$ find-type f-printf '%t+%pn ' | Sort | Head-n 1

2006-02-03+02:40:33./skel/.xemacs/init.el

$ find-type f-printf '%t+%pn ' | Sort | Tail-n 1

2015-07-19+14:20:16./.bash_history

printf commands output%T (file date and time) and%P (filename with path) parameters.

If we are looking for a home directory, we will no doubt find that history files (such as. bash_history) are up to date, which is of little use. You can ignore these files by "un-grepping", or you can ignore the file that starts with the., as shown in the following figure.

The code is as follows:

$ find-type f-printf '%t+%pn ' | Grep-v "./." | Sort | Tail-n 1

2015-07-19+13:02:12./isprime

Find the largest file using the%s (size) parameter, including the filename (%f), because that's what we want to show in the report.

The code is as follows:

$ find-type f-printf '%s%f n ' | Sort-n | Uniq | Tail-1

20183040 Project.org.tar

Owner of the statistics file, using%u (owner)

The code is as follows:

$ find-type f-printf '%u n ' | Grep-v "./." | Sort | Uniq-c

180034 SHS

7500 JDoe

It would also be useful to have the file system record the last access date, which could be used to see if the file has been accessed, for example, within two years. This will enable you to distinguish clearly the value of these documents. This last access (%a) parameter is used in this way:

The code is as follows:

$ find-type f-printf '%a+%pn ' | Sort | Head-n 1

Fri Dec 03:00:30 2006+./statreport

Of course, if most of the recently accessed files were also a long time ago, it looks like you need to process more files.

The code is as follows:

$ find-type f-printf '%a+%pn ' | Sort | Tail-n 1

Wed Nov 03:00:27 2007+./my-notes

To be structured, you can create a summary report for a file system or a large directory, showing the date range of these files, the largest files, the file owners, the oldest files, and the latest access time, to help the file owner determine which folders are important and which to clean up.

How to temporarily empty Bash environment variables before running commands on Linux

How do I display the current environment?

Open the terminal application and enter one of the following commands:

The code is as follows:

Printenv

Or

The code is as follows:

Env

Output Sample:

Number of statistical environment variables

Enter the following command:

The code is as follows:

env | Wc-l

printenv | Wc-l # or

Output Sample:

The code is as follows:

20

Running programs in a clean BASH/KSH/ZSH environment

The syntax looks like this:

The code is as follows:

Env-i your-program-name-here arg1 arg2 ...

For example, to run a wget program without using http_proxy and/or any other environment variable. Temporarily clear all BASH/KSH/ZSH environment variables and run the wget program:

The code is as follows:

Env-i/usr/local/bin/wget www.cyberciti.biz

Env-i wget www.cyberciti.biz # or

This is useful when you want to ignore any set of environment variables to run the command. I use this command many times a day to ignore http_proxy and other environment variables that I set up.

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 ... 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: Ignoring Http_proxy

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 ... 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 (kb/s)-' index.html.1 ' saved [37041]

The-i option causes the ENV command to completely ignore the environment it inherits. However, it does not prevent your commands (such as wget or curl) from setting new variables. Also, be aware of the side effects of running the Bash/ksh shell:

The code is as follows:

Env-i env | Wc-l # # empty # #

# now run Bash # #

Env-i Bash

# # Bash sets new environment Variables # #

env | Wc-l

Example: Setting an environment variable

The syntax is as follows:

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:

The code is as follows:

Env http_proxy= "http://user:password@server1.cyberciti.biz:3128/"/usr/local/bin/wget www.cyberciti.biz

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.