Sunday 4 August 2013

Unix Commands

In this post I catalogue most of the Unix commands that I have been using on a frequent basis.

-bash$ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

List of commands you use most often

-bash$ ssh -X user@host
ssh remote login with X11 forwarding

-bash$ strace -ff -e trace=write -e write=1,2 -p SOME_PID
intercept stdout/stderr of another process

-bash$  strace -p PID
print stack trace for process PID

-bash$  ls -l /proc/PID/fd
list the file handles that process PID has open.

-bash$  Ctrl + z
-bash$  bg
-bash$  disown -h [job-spec]
pause already running process, put into background, and detach.

-bash$  top - u user
list running processes for user. -bash$ echo "ls -l" | at 1800
Execute a command at a given time

-bash$ man ascii 
quick access to the ascii table

-bash$ !!:gs/foo/bar 
Runs previous command replacing foo by bar every time that foo appears

-bash$ curl ifconfig.me
get your external ip address

-bash$ sshfs name@server:/path/to/folder /path/to/mount/point
Mount folder/filesystem through SSH

-bash$ ctrl-l 
clear the terminal screen

-bash$ (cd /tmp && ls)
Jump to a directory, execute a command and jump back to current dir

-bash$ du -h --max-depth=1 | sort -n
List the size (in human readable form) of all sub folders from the current location, sorted by size.

-bash$ disown -a && exit
Close shell keeping all subprocess running

-bash$ ps aux | sort -nk +4 | tail
Display the top ten running processes - sorted by memory usage

-bash$ find . -name "*.[py]" -exec grep -i -H "search pattern" {} \;
Search recursively to find a word or phrase in certain file types, such as Python code.
-H tells grep to print the filename. 
-i to match the case exactly.
-bash$ awk '/start_pattern/,/stop_pattern/' file.txt
Display a block of text with AWK.

-bash$ grep -A # pattern file.txt"
to see a specific number of lines following your pattern

-bash$ grep -n --color pattern * -r


-bash$ lsof -i
Watch Network Service Activity in Real-time

-bash$ some_very_long_and_complex_command # label
Using the above trick it's possible to label your commands and access them easily by pressing ^R and typing the label

-bash$ lsof -P -i -n
Show apps that use internet connection at the moment.

-bash$ mv filename.{old,new}
quickly rename a file

-bash$ rm !(*.foo|*.bar|*.baz)
Delete all files in a folder that don't match a certain file extension

-bash$ wget -qO - "http://www.tarball.com/tarball.gz" | tar zxvf -
Extract tarball from internet without local saving

-bash$ cat /etc/issue
Display which distro is installed

-bash$ man hier
Show File System Hierarchy

-bash$ sudo dd if=/dev/mem | cat | strings
this command will show you all the string (plain text) values in ram

-bash$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
Graphical tree of sub-directories

-bash$ mkdir -p a/long/directory/path
Make directory including intermediate directories

-bash$ sed -n '10,20p' 
Print all the lines between 10 and 20 of a file


-bash$ pv sourcefile > destfile
Copy a file using pv and watch its progress

-bash$ rename 'y/ /_/' *
replace spaces in filenames with underscores

-bash$ vim scp://username@host//path/to/somefile
Edit a file on a remote host using vim

-bash$ du -s * | sort -n | tail
Get the 10 biggest files/folders for the current directory

-bash$ mkdir /home/foo/doc/bar && cd $_
mkdir & cd into it as single command

-bash$ chmod --reference file1 file2
Makes the permissions of file2 the same as file1

-bash$ sed -n 5p 
To print a specific line from a file

-bash$ tar -tf  | xargs rm -r
Remove all files previously extracted from a tar(.gz) file.

-bash$ ls -d */
list directory entries instead of contents, and do not dereference symbolic links

-bash$ ps awwfux | less -S
Show a 4-way scrollable process tree with full details.

-bash$ find . -type d -empty -delete
Recursively remove all empty directories

-bash$ leave +15 ; leave 1855
set an alarm at for a particular time

-bash$ grep ^Dirty /proc/meminfo
Find out how much data is waiting to be written to disk

-bash$ mkdir -p work/{d1,d2}/{src,bin,bak}
make directory tree

-bash$ cp file.txt{,.bak}
Create a quick back-up copy of a file

-bash$ echo ${SSH_CLIENT%% *}
Get the IP of the host your coming from when logged in remotely

-bash$ showkey -a
Quick access to ASCII code of a key

-bash$ history -d
delete a line from your shell history

-bash$ find ./ -type f -exec chmod 644 {} \;
Recursively change permissions on files, leave directories alone.

-bash$ alias dush="du -sm *|sort -n|tail"
easily find megabyte eating files or directories. dush for the above output. dush -n 3 for only the 3 biggest files

-bash$ :mksession!  
Save your sessions in vim to resume later

-bash-3.2$  ./myapp &> log.txt & disown -h ;  tail -f log.txt
Run background process. Redirect stdout and stderr to log.txt, disown the process, and tail the log file.

-bash-3.2$  tar -zxvf data.tar.gz
where the options zxvf are:
-z : Uncompress the resulting archive with gzip command.
-x : Extract to disk from the archive.
-v : Produce verbose output i.e. show progress and file names while extracting files.
-f data.tar.gz : Read the archive from the specified file called data.tar.gz.


-bash-3.2$  ls -lhs
List files by size, with option l for a long listing format, h for human readable file size, and s to indicate sort by size.

-bash-3.2$  ls -lSr
To list in reverse order.

-bash-3.2$ wc -l *
Count the number of rows in each file in the current directory.

-bash-3.2$  ls -1 targetdir | wc -l
Count the number of files in the current directory

 -bash-3.2$  top
List running processes. Press 1 to list cpus.

 -bash-3.2$  find /home/me | grep foo.c
Locate file foo.c

 -bash-3.2$ netstat -tlnp
Lists all listening ports together with the PID of the associated process

 -bash-3.2$ mrt HOSTNAME
Better than traceroute and ping combined. Investigates the network connection between the host mtr runs on and HOSTNAME.

 -bash-3.2$ > file.txt
flush the contents of a file without deleting it

 -bash-3.2$ Ctrl-r
Allows you to reverse search through your command history.

 -bash-3.2$ du -skh .
Outputs the total size of files and directories within the current directory?

 -bash-3.2$ df -kh .
Outputs the used and available space for the current drive.

No comments:

Post a Comment