Auto classification Example
Created Thursday 03 limit l 2014
So, we can we up.
When I first came into contact with Shell, I suddenly thought that my Download directory was the default Download directory. Every time I put different types of files in a folder, it looked messy and my eyes were dizzy, for example, put the zip format in the same directory and the xx format in the xx directory. Manual music videos are too troublesome for similar scenarios. So try it. Because there are not many files changed in the system, some files are moved out... the Shell level is too good to laugh.
Code
Version 0.1:
#/Bin/bashLUJING = $1 # indicates the echo $ ###$ # directory to be sorted, indicating the number of parameters, which is irrelevant to the following functions. It's just a look. # define the function for moving files. Function move_file () {# Three parameters. The first parameter is the path, and the second parameter is the file type, the third is to remember the directory name file_name = $ 1file_type = $ 2dir_name = $3 # Use grep below to determine that the type suffix is not in the file name, for example. txt echo $ file_name | grep-q $ file_typeif [$? -Eq 0]; then # If grep is found, there is EXITVALUE = 0, indicating that it is found. If not, it is equal to 1 # $? The EXITVALUE of the last command. Success is 0, and failure is 1if [! -D $ LUJING/$ dir_name]; then # create mkdir-p $ LUJING/$ dir_namefi if the directory already exists. # Move the file to the folder, OK mv-fv $ LUJING/$ I $ LUJING/$ dir_name/fi} for I in 'ls $ LUJING '{# determine whether the file is true if [-f $ LUJING/$ I ]; then # Does not intercept the following information. Only the grep method can be used for search. Move_file $ I ". pdf "" PDF "move_file $ I ". exe "" EXE "move_file $ I ". epub "" EPUB "move_file $ I ". zip "" ZIP "move_file $ I" .tar.gz "" TAR_GZ "move_file $ I ". iso "" ISO "move_file $ I ". 7z "" _ 7z "move_file $ I ". jpg "" Pic "move_file $ I ". rpm "" RPM "move_file $ I ". txt "" TXT "move_file $ I ". html "" HTML "# add others to unclassified move_file $ I ". "" NotClassFile "fi}
Effect
[yg@localhost Downloads]$ ls4df3c207e5ba15f649b7ee55de422d5c.jpg README590896715b45fe09c8b801132a9b367a.jpg readme.txtdadffe.tar.gz test.txtgoagent-goagent-v3.1.6-70-gc761281.zip xx.pdfLinux Shell Scripting Tutorial v2.0.pdf zimlib-devel-1.0-6.fc20.x86_64.rpmok.txt[yg@localhost Downloads]$ ~/Shell/example/auto_extract.sh .[yg@localhost Downloads]$ lsLinux Shell Scripting Tutorial v2.0.pdf NotClassFile PDF Pic RPM TAR_GZ TXT ZIP[yg@localhost Downloads]$ tree -L 2 ..├── NotClassFile│ └── README├── PDF│ ├── linux_shell_scripting.pdf│ └── xx.pdf├── Pic│ ├── 4df3c207e5ba15f649b7ee55de422d5c.jpg│ └── 590896715b45fe09c8b801132a9b367a.jpg├── RPM│ └── zimlib-devel-1.0-6.fc20.x86_64.rpm├── TAR_GZ│ └── dadffe.tar.gz├── TXT│ ├── ok.txt│ ├── readme.txt│ └── test.txt└── ZIP └── goagent-goagent-v3.1.6-70-gc761281.zip
Author: YangGan
Source: http://blog.csdn.net/incyanggan
This article was published based on the signature 2.5 mainland China license agreement. You are welcome to repost, interpret, or use it for commercial purposes, but you must keep the signature Yanggan (including links) in this article ).