Tips Bash
#Linux rights
Rights r w x
Owner 400 200 100
Group 40 20 10
Others 4 2 1
#find jpg mv
find . -name "*.jpg" -exec mv {} . \;
#Search & Replace in files with command-line (-name optional)
find . -name *.php -exec sed -i s@require_once@//require_once@g {} \;
#applying a patch
Place the .diff file in the same directory as the source tarball. Ungzip/untar the source, and run
patch -p0 < some-patch-file.diff
The -p0 indicates the paths in the file are relative to the current directory, if you place the .diff file in the some-soft-source dir you need -p1.
#Remove orphaned configuration files
# aptitude purge $(dpkg --get-selections | grep deinstall | awk '{print $1}')
#List the files with a different MD5 checksum
find . -maxdepth 1 -type f -print0 | xargs -0 md5sum | sort | uniq -w 32 | awk '{print $2}' > ./distinct.txt
#pdf to jpg
Each page as an image :
convert file.pdf image%d.jpg
#grep
dpkg -l | grep '(15|16)' | grep linux
#multiple egrep
cat file.txt | egrep -i '(pattern1|pattern2)'
#optimise firefox sqlite
find ~/.mozilla/firefox/ -type f -name "*.sqlite" -exec sqlite3 {} VACUUM \;
#cp dir1 > dir2
cp -ruv /dir1/ /dir2/
The options allow you to display the copied files, copy entire directories, and update files that have already been copied.
#list dir
find . -type d
#cpu load in %
top -b -n 1 | grep Cpu | awk '{print $2}' | cut -c1-4
#capture tty
fbgrab capture.png
#add a word to the beginning of each line
for i in `cat file.txt`Β ; do word_to_add $iΒ ; done
#cat without duplicate
cat file1.txt file2.txt | sort -n | uniq -u
#extract only the parts that are "readable" with strings
strings file.doc | less
and
strings -e b file.doc | less
#iso to utf
iconv -f ISO-8859-1 -t UTF-8 -o output.txt input.txt
iconv -f UTF-8 -t ISO-8859-1 -o output.txt input.txt
recode ISO-8859-15..UTF-8 input.txt
#sum with awk
For example, to determine the amount of memory being used by process apache2:
ps -ely | grep '\<apache2\>' | awk '{SUM += $8} END {print SUM}'
or to find out the total size in kilobytes occupied by all the PNG files in the directory:
ls -l *.png | awk '{SUM += $5} END {print SUM/1024}'
#Displays the most recent file .txt
ls -t1 *.txt | head -1
#Displays all .txt files except for the file named "file.txt"
ls *.txt | grep -v file.txt | xargs ls
#Display all .txt files except for the last .txt file
ls *.txt | grep -v `ls -t1 *.txt | head -1`
#sed syntax for displaying the text between "pattern1" and "pattern2":
sed -n '/pattern1/,/pattern2/p' /path/to/file
#Keyboard shortcuts for Bash
Some of these commands also work within command-line file editors. For example, Emacs offers movement commands and copy/paste functionality
1. Move
Ctrl + a: Go to the beginning of the line
Ctrl + e: Go to the end of the line
Alt + b: Move word by word backward in the command line (b for backward)
Alt + f: Move word by word forward in the command line (f for forward)
Ctrl + xx: Position the cursor at the beginning or end of the word
2. Cut/Paste
Ctrl + k: Cut the string from the cursor to the end of the line
Ctrl + u: Cut the string from the cursor to the beginning of the line
Ctrl + w: Cut the word before the cursor
Ctrl + y: Paste a string
3. Modification
Ctrl + t: swap the position of the two characters before the cursor (useful when typing, for example, sl instead of ls)
Alt + t: swap the position of the two words before the cursor
Alt + c: capitalize a letter
Alt + l: convert a word to lowercase (l for lowercase)
Alt + u: capitalize a word (u for uppercase)
Alt + .: rewrite the parameter of the last command
4. Miscellaneous
Ctrl + l: Clear the screen
Ctrl + r: Search for a previously typed command
Ctrl + -: Undo the last change
Ctrl + c: Stop the current command
Ctrl + d: Exit the current shell