LINUX_TOOLS.BASH
Sort in Reverse -r
Numerical Sort -n ex sort -n filename
Sort Ignore Case -f
Sort Ignore Blanks -b ex sort -b filename
Sort Stable -s
Sort Unique -u ex sort -u filename
#IF IT DOSNT SORT RIGHT : export LC=ALL
Pipe sort into uniq sort [options] filename | uniq
##########################
Sort Unique / Case Sensitive sort -s -u
Sort by Column (Blank Separators) sort -k5 OR sort -k5,5
Sort by Column (Other Separator) sort -t(separator) -k4
Sort by 2 columns sort -k 2,2 -k 4,4
Sort by 2 columns / numerical sort -k 2,2n -k 4,4n
Sort by substring 1 column sort -k 2.1,2.13
Sort by substring multiple columns sort -k 2.3,2.7 -k 3.1,3.11
Sort and Randomize cat filename | sort -R
Sort with Multiple File Input sort [options] filename 1 filename2 (appends)
Sort with Multiple File Merging sort -m filename1 filename2
Sort (Check if Sorted) sort -n -c filename
Head - Standard Syntax head [options] [file(s)]
Head - Multiple File input head [options] [file(s)] [file(s)]
Head - First 15 lines head -15 file OR head -n 15 file
Head - First 5k bytes head -c 5 file OR head -c5k file
Head - Remove last four lines head -n -4 file
########################
Tail - Standard Syntax tail [options] [file(s)]
Tail - Output 15 last lines tail -n 15 filename
Tail - Last 15k bytes tail -c15k filename
Tail and monitor for file updates tail -f myfile
Tail - Remove first 4 lines tail -n +4 myfile
1. sort : Utility to sort files
2. head / tail : Utility to extract first / last lines
find ./source_dir -name *.png | xargs cp --target-directory=./destination_dir
find ./source_dir -name *.png | xargs cp --target-directory=./destination_dir
This will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.
Code:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
or
Code:
find ./source_dir -name \*.png -exec cp -t path/to/destination '{}' +
find location comparison-criteria search-term
1. List all files in current and sub directories
find
find .
2. Search specific directory or path
find ./test
find ./test -name "abc.txt"
find ./test -name "*.php"
find ./test -iname "*.Php" (case insensitive)
3. Limit depth of directory traversal
find ./test -maxdepth 2 -name "*.php"
4. Invert match
find ./test -not -name "*.php"
5. Combine multiple search criterias
find ./test -name 'abc*' ! -name '*.php'
6. OR operator
find -name '*.php' -o -name '*.txt'
7. Search only files or only directories
find ./test -name abc*
find ./test -type f -name "abc*"
find ./test -type d -name "abc*"
8. Search multiple directories together
find ./test ./dir2 -type f -name "abc*"
9. Find hidden files
find ~ -type f -name ".*"
10. Find files with certain permissions
find . -type f -perm 0664
find . -type f ! -perm 0777 (inversion, not)
10. Find files with sgid/suid bits set
find / -perm 2644
find / -maxdepth 2 -perm /u=s 2>/dev/null
11. Find readonly files
find /etc -maxdepth 1 -perm /u=r
12. Find executable files
find /bin -maxdepth 2 -perm /a=x
13. Find files owned to particular user
find . -user bob
find . -user bob -name '*.php'
14. Search files belonging to group
find /var/www -group developer
14. Search home directory
find ~ -name "hidden.php"
15. Find files modified N days back
find / -mtime 50
16. Find files accessed in last N days
find / -atime 50
17. Find files modified in a range of days
find / -mtime +50 –mtime -100
18. Find files changed in last N minutes.
find /home/bob -cmin -60
19. Files modified in last hour
find / -mmin -60
20. Find Accessed Files in Last 1 Hour
find / -amin -60
21. Find files of given size
find / -size 50M
22. Find files in a size range
find / -size +50M -size -100M
Find largest and smallest files
find . -type f -exec ls -s {} \; | sort -n -r | head -5
find . -type f -exec ls -s {} \; | sort -n | head -5
Find empty files and directories
find /tmp -type f -empty
find ~/ -type d -empty
25. List out the found files
find . -exec ls -ld {} \;
26. Delete all matching files or directories
find /tmp -type f -name "*.txt" -exec rm -f {} \;
find /home/bob/dir -type f -name *.log -size +10M -exec rm -f {} \; (with size)