Technical Documentation Site

Version 0.9.0

Technical Cheat Sheets

Bash Shell Commands

ls

list files and directories

ls

options:
-l  long format
-a --all. Lists all files in a directory, including hidden files
-R --recursive. List subdirectories 
-lh --human-readable  
find

find a file named myfile.txt in dir /home/mydir

find /home/mydir -name myfile.txt

find a file named myfile.txt in current directory

find . -name myfile.txt
grep

search string abc within directory named mydir and its sub directories

    grep -r abc mydir 

search string abc within current directory its sub directories

    grep -r abc .

count number of lines returned by grep commnad

    grep abc file.txt | wc -l

search string abc and display 2 lines before and 3 lines after the search result

    grep -b2 -a3 abc file.txt 

inverted search. exclude the string bar from the search result

    grep -v bar /path/to/file

save the results of grep to a file ~/tmp/results.txt

    grep -r abc mydir > ~/tmp/results.txt

Note: Do not create the output file into the same folder where you are recursively grepping into.

sort the fifth column of the grep results and save the sorted result to a file

grep 'Conversion completed in' /var/log/tomcat5/catalina.out | sort  -k 5,5n > conversion-time.txt

example:

input
INFO: Conversion completed in: 1351 ms
INFO: Conversion completed in: 413 ms
INFO: Conversion completed in: 2220 ms

output
INFO: Conversion completed in: 413 ms
INFO: Conversion completed in: 1351 ms
INFO: Conversion completed in: 2220 ms

display first 10 lines from a file

head -n 10 file.txt

create new target:

     ln -s target shortcut   
     ln -s tomcat6.0.32 tomcat

change existing target

    ln -f -s tomcat6.0.35 tomcat

remove symlink

    rm tomcat
disk info

Check partition sizes

df -h

Check directory size

du -s -h /var/log/

Check individual file size

du -s -h /var/log/file.txt
sed

replace all occurances of word1 in input.file with word2 and save results to output.file

sed 's/word1/word2/g' infile.txt > outfile.txt

remove trailing spaces from text file

sed -i 's/[ \t]*$//' file.txt

display lines from number 2000 to 2004 from a file

sed -n 2000,2004p file.txt

Insert characters at begining of each line:

  sed 's/^/abc/' file.txt
ssh
ssh username@<hostname>
password:

ssh verbose mode:

    ssh -v user@hostname

tail ssh log file

    sudo tail -f /var/log/secure

start/stop/restart sshd server

    /etc/init.d/sshd stop 
    /etc/init.d/sshd start 
    /etc/init.d/sshd restart

How to resolve SSH authentication refused error: http://www.daveperrett.com/articles/2010/09/14/ssh-authentication-refused/

scp

You can use scp to recursively transfer files between computers

transfer from localdir to remotedir

scp -r localdir username@hostname:/path-to-remotedir

Recursively download directory from a remote computer to current directory. This will create the a folder with the name of the remote dir on your local machine and add any sub folders and files to it

scp -r username@hostname:/path-to-remote-dir .
Change shell

Change the shell to bash

chsh -s /bin/bash
Last updated on 5 May 2020
Published on 5 May 2020
Edit on GitHub