Most Useful Bash and Git Commands

5 minute read

File Operations                                                                                 
ls list files without hidden
ls -a list files including hidden
ls -l list files with long details
ls -s <file> <link> create symbolic link to file
touch <file> create a new file
mkdir <dirname> make a new directory
cd change to home
cd <dirname> change to directory
pwd tell current directory location
<command> > <file> write standard output to file
<command> >> <file> append standard output to file
cat > <file> write standard input to file
cat >> <file> append standard input to file
log <file> write both standard input and output to file
more <file> show the first part of file (move with space and type q to quit)
head <file> output the first 10 lines of file
tail <file> output the last 10 lines of file (useful with -f option)
emacs <file> let you create and edit a file
mv <file1> <file2> move a file
cp <file1> <file2> copy a file
rm <file> remove a file
diff <file1> <file2> compare files and show differences
wc <file> show word count of file
chmod -options <file> change the read, write, and execute permissions on files
gzip <file> compress file
gunzip <file> uncompress file
gzcat <file> look at gzipped file without actually having to gunzip it
grep <pattern> <file> search a string in file
grep -i "str1 /| str2 /| ..." <file> search multiple strings in file
grep -i "str1 /| str2 /| ..." -B 3 -A 4 <file> search multiple strings in file and print occurance lines with surrounding lines (3 lines before occurance and 4 lines after occurance)
grep -r <pattern> <dir> search recursively for pattern in directory
Network & Process Operations                                                                                 
ssh user@host connect to remote host via default SSH port (22)
ssh user@host -p <port> connect to remote host via custom SSH port
ssh-copy-id user@host add your ssh key to host in order to enable a keyed or passwordless login
w display whois online
whoami return your username
passwd change your password
uname -a show kernel information
df show disk usage
date show the current date and time
du <file> show the disk usage of the files and directories in file (du -s give only a total)
last <username> list last logins from user
ps print a line of information about the current running login shell and any processes running under it
ps -a select all processes with a TTY, except session leaders
ps -u <username> lists user processes
kill -l return a list of all signals on the system, by name and number
kill <PID> kill process
kill -9 <PID> kill process (SIGKILL)
kill -19 <PID> freeze process (SIGSTOP)
killall <processname> kill all processes with the name
top display currently active processes
ping <host> ping host and outputs results
whois <domain> get whois information for domain
dig <domain> get DNS information for domain
dig -x <host> reverse lookup host
wget <file> download file
cmd1 && cmd2 AND operator
cmd1 || cmd2 OR operator
cmd1|cmd2 pipe; take standard output of cmd1 as standard input to cmd2

SCP

SCP (Secure Copy Protocol) is the most popular file transferring method in Linux server environments. You can use the following commands to transfer files and directories between servers.

1. From Local To Remote

a) Transfer a file from Local Machine to Remote Server

scp [file name] [destination user]@[destination server]:[destination file location]

$ scp logs.tar.gz rhelVM@192.168.8.145:~/dev/

b) Transfer a directory from Local Machine to Remote Server

scp -r [directory name] [destination user]@[destination server]:[destination directory location]

$ scp -r ~/logs rhelVM@192.168.8.145:~/dev/

2. From Remote To Local

a) Transfer a file from Remote Server to Local Machine

scp [source user]@[source server]:[source file location]/[file name] .

$ scp rhelVM@192.168.8.145:~/dev/logs.tar.gz .

b) Transfer a directory from Remote Server to Local Machine

scp -r [source user]@[source server]:[source directory location]/[directory name] .

$ scp -r rhelVM@192.168.8.145:~/logs .

3. From Remote To Remote

a) Transfer a file from Remote Server to another Remote Server

scp [source user]@[source server]:[source file location]/[file name] [destination user]@[destination server]:[destination file location]

$ scp debianVM@192.168.8.133:~/dev/logs.tar.gz rhelVM@192.168.20.145:~/logs

b) Transfer a directory from Remote Server to another Remote Server

scp -r [source user]@[source server]:[source directory location]/[directory name] [destination user]@[destination server]:[destination directory location]

$ scp -r debianVM@192.168.8.133:~/logs rhelVM@192.168.20.145:~/dev

~/.bashrc

alias c='clear'

# if user is not root, pass all commands via sudo #
if [ $UID -ne 0 ]; then
    alias reboot='sudo reboot'
    alias update='sudo apt-get upgrade'
fi
## Colorize the ls output ##
alias ls='ls --color=auto'

## Use a long listing format ##
alias ll='ls -la'

## Show hidden files ##
alias l.='ls -d .* --color=auto'
## get rid of command not found ##
alias cd..='cd ..'

## a quick way to get out of current directory ##
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias path='echo -e ${PATH//:/\\n}'
alias now='date +"%T"'
alias nowtime=now
alias nowdate='date +"%d-%m-%Y"'
alias vi=vim
alias svi='sudo vi'
alias vis='vim "+set si"'
alias edit='vim'
# do not delete / or prompt if deleting more than 3 files at a time #
alias rm='rm -I --preserve-root'

# confirmation #
alias mv='mv -i'
alias cp='cp -i'
alias ln='ln -i'

# Parenting changing perms on / #
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'
## pass options to free ##
alias meminfo='free -m -l -t'

## get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'

## get top process eating cpu ##
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'

## Get server cpu info ##
alias cpuinfo='lscpu'

## older system use /proc/cpuinfo ##
##alias cpuinfo='less /proc/cpuinfo' ##

## get GPU ram on desktop / laptop##
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'

Tags: ,

Updated:

Leave a comment