Linux provides many commands for finding, counting, and renaming files. There are some useful options.

Linux provides a variety of commands for manipulating files that can save you time and make your work less tedious.

Find files

Find is probably the first command that comes to mind when you’re looking for a file, but sometimes a well-designed ls command is better. Want to know the script you called yesterday before you left the office for home? Simple! Use the ls command with the -ltr option. The last one listed will be the most recently created or updated file.

$ ls -ltr ~/bin | tail -3
-rwx------ 1 shs shs   229 Sep 22 19:37 checkCPU
-rwx------ 1 shs shs   285 Sep 22 19:37 ff
-rwxrw-r-- 1 shs shs  1629 Sep 22 19:37 test2
Copy the code

A command like this will list only files updated today:

$ ls -al --time-style=+%D | grep `date +%D` drwxr-xr-x 60 shs shs 69632 09/23/19 . drwxrwxr-x 2 shs shs 8052736 09/23/19  bin -rw-rw-r-- 1 shs shs 506 09/23/19 statsCopy the code

If the file you are looking for may not be in the current directory, find will provide a better option than LS, but it may yield more results than you want. In the following command, we don’t search directories that start with a dot (many of which are constantly updated), specify that we are looking for files (that is, not directories), and ask that only files that have been updated in the last day (-mtime-1) be displayed.

$ find . -not -path '* / \. *' -type f -mtime -1 -ls
   917517      0 -rwxrw-r--   1 shs      shs          683 Sep 23 11:00 ./newscript
Copy the code

Note that the -not option reverses the behavior of -path, so we do not search for subdirectories that begin with a dot.

If you only want to find the largest files and directories, you can use a command like du, which lists the contents of the current directory by size. Pipe the output to tail and view only the largest ones.

$ du -kx | egrep -v "\. / + /" | sort -n | tail -5
918984      ./reports
1053980     ./notes
1217932     ./.cache
31470204    ./photos
39771212    .
Copy the code

The -k option lets du list file sizes in blocks, while x prevents it from traversing directories on other file systems (for example, by symbolic link reference). In fact, du lists the file sizes first so you can sort them by size (sort -n).

File count

The find command makes it easy to count files in any particular directory. Just remember that find recurses into subdirectories and counts the files in those subdirectories along with the files in the current directory. In this command, we count the files in the home directory of a particular user (username). Depending on the permissions of your home directory, this may require sudo. Remember that the first parameter is the starting point for the search. This specifies the user’s home directory.

$ find ~username -type f 2>/dev/null | wc -l
35624
Copy the code

Note that we are sending the error output from the find command above to /dev/null to avoid searching for folders such as ~username/. Cache that are not searchable and have no interest in its contents.

If necessary, you can restrict find to a single directory using the maxdepth 1 option:

$ find /home/shs -maxdepth 1 -type f | wc -l
387
Copy the code

Rename file

It’s easy to rename files using the mv command, but sometimes you want to rename a lot of files and you don’t want to spend a lot of time. For example, to change all Spaces you find in the filename of the current directory to underscores, you can use the following command:

$ rename 's/ /_/g' *
Copy the code

As you might suspect, the G in this command stands for “global.” This means that the command changes all Spaces in file names to underscores, not just the first one.

To remove the.txt extension from a text file, use the following command:

$ rename 's/.txt//g' *
Copy the code

conclusion

The Linux command line provides many useful options for working with files. Please suggest any other commands that you find particularly useful.


Via: www.networkworld.com/article/344…

Author: Sandra henry-stocker lujun9972

This article is originally compiled by LCTT and released in Linux China