In the past few weeks, I have always received people's questions about fast and scattered issues. Sometimes a person is looking for a helpful suggestion, a specific command, or a command line shortcut to complete a specific task. This article will introduce three questions recently raised to me and a simple way to complete these tasks.
In the first scenario, we need to find the files modified in the past month and copy them from the current location to another directory.
In this case, the exact solution depends on whether the original file tree structure is maintained when you copy the file, or whether you just copy all the files to a specified place. If we just want to save all the files to a folder, or we want to archive these files, we can use the find command. By using the find command, we can find their location based on the time when the file was modified, and then copy them to the specified location. This command looks like this:
Find Documents-mtime-30-exec cp "{}" Backup \;
The above command will search for the files modified in the last 30 days in the Documents folder. These files will be copied to another directory named Backup. The find command will execute the copy operation by calling the cp command. By modifying the mtime parameter, we can find the file that has been modified recently. In the above example, it is set to within the past 30 days.
More often, we want to keep the directory structure of the source folder after copying the file to the target folder. Most of the time, people want to synchronize the content of the two folders, and then they will run a script to keep them consistent. In this case, we may use the rsync command. This tool copies new files and modified files from one directory to another, while maintaining the display structure of the source directory:
Rsync-a Documents ents/Backup
In the above example, it will first find which files exist in both Documents and Backup, so that unnecessary files are no longer copied.
In the second scenario, one common task we will do is to find a word in a text file and use another word to replace all the examples of the word in the text.
For example, assume that I mentioned a person named "Becky" in a document, but I decided to use the formal name "Rebecca" later. For this simple change in the file, the following command will play a role:
Perl-pi-e's/Becky/Rebecca/'mydocument.txt
This micro Perl script queries all the "Becky" instances in the document and changes it to "Rebecca ". The program first reads the document whose name is mydocument.txt and then saves it after modification.
A common concern in the third scenario is what to do with the sensitive data stored in a hard disk before it is intended for others or thrown away.
Some people will save some banking business data or tax information on their computers, so it is best to clear the data before your hard disk is transferred to others. There are some methods and tools for you to use. One of my favorites is the shred command, which not only processes a single file, but also processes the entire device. To rewrite the content in a file, run shred as follows:
Shred mytaxes. odt
In the above example, it will remove the content stored in the file, but will not delete the file itself from the hard disk. If you want to clear the file content and delete the file at the same time, run the following command:
Shred-u mytaxes. odt
It should be emphasized that shred does not run normally in all file systems, especially those newer file systems with log functions. In the shred operation manual, you can find out which file systems run the program. When processing sensitive data, the best way is to clear the entire hard disk. Shred can do the same. The following example shows how to remove all data from the first hard disk on our computer. Be careful when using this command:
Shred/dev/sda
Via: http://distrowatch.com/weekly.php? Issue = 20130923
This article was originally translated by LCTT and launched with the Linux honor in China
Translator: markvv Proofreader: wxy
Recommended reading:
Linux Command cd
Cat for Linux commands
Linux Command alias/unalias
Linux Command Parsing: su root and su-root
Interactive input of Linux commands read
File Processing commands for Linux commands