Linux Command Models

Display the current directory sorted by time, in reverse order. ls -ltr
Count files in the current directory. ls -l | wc -l
Display files between 1 and 2 days old. find . -ctime 1 | xargs ls -l
Display files more than 2 days old. find . -ctime +2 | xargs ls -l | less
Delete unconditionally files older than 2 days. find . -ctime +2 | xargs rm -f
Display files modified between 6 and 9 minutes ago. find . -mmin +5 -mmin -10 | xargs ls -l
Move files created more than 1020 minutes ago from the current directory into the 20070512/ directory . find . -cmin +1020 | xargs -i mv {} 20070512/
List files between 1200 minutes and 1600 minutes old, matching the pattern index* . find . -mmin +1200 -mmin -1600 -name 'index*' | xargs ls -l
To a subdirectory depth of 3, find files in subdirectories containing 'abc' in their names and having a size greater than 200 bytes. find . -maxdepth 3 -type f -name "*abc*" -size +200c
To a subdirectory depth of 3, find files in subdirectories with a name of target and a size greater than 200 characters, and rename them to target.large . find . -maxdepth 20 -type f -name "target" -size +200c -exec mv "{}" "{}".large \;
For a maximum subdirectory depth of 20, find files named index.php, index.php3, index.html, or index.htm that contain the text "<script>function" and write what is found to output file "script_functions.txt" (finds certain kinds of injected spam). find ./ -maxdepth 20 -type f \( -name "index.php" -o -name "index.php3" -o -name "index.html" -o -name "index.htm" \) -exec grep -H --line-buffered "<script>function" '{}' >> ./script_functions.txt \;
In the current directory, find files with names beginning "__132." and pass each one to grep, to search for the string "<script>function" within each file pasted from the find command. find . -name "__132.*" -exec grep -i '<script>function' "{}" \;
rename every file in the directory to only the second and third nodes delimited by ".", dropping the first node in the filename ls -1 ./ | awk -F. '{print "mv "$1"."$2"."$3" "$2"."$3 }' | sh
display a scrollable list of filenames and occurrances of string "google_argument" find . -maxdepth 1 -type f -exec grep -H 'google_argument' "{}" \; | less
display a scrollable list of filenames only, in which match occurs find . -maxdepth 1 -type f -exec grep -l 'google_argument' "{}" \; | less
find files containing 'google_argument' and output to a file a list those which do not have a tilde in the filename find . -maxdepth 1 -type f -exec grep -l 'google_argument' "{}" \; | grep -v '~' > ~/Desktop/google_argument_use.txt
find files with neither "~" nor ".prod." in the filename, and containing 'google_argument' in the file, and output the list to a file find . -maxdepth 1 -type f -exec grep -l 'google_argument' "{}" \; | grep -v '~' | grep -v '.prod.' > ~/Desktop/google_argument_use.txt
same as above, but not containing "$google_argument" find . -maxdepth 1 -type f -exec grep -l 'google_argument' "{}" \; | grep -v '~' | grep -v '.prod.' | xargs grep -H -n '[^\$]google_argument' > ~/Desktop/google_argument_use.txt
search the current directory for any file containing the string 'get_row_for_page' and outputs the list to a file on the Desktop find . -maxdepth 1 -type f -exec grep -l 'get_row_for_page' "{}" \; | grep -v '~' | grep -v '.prod.' | xargs grep -H -n 'get_row_for_page' > ~/Desktop/get_row_for_page_use.txt
[from the n directory's parent directory] move everything inside the n directory out one level in the directory tree. mv ./n/* ./
show the directory tree structure ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'