mkdir -p /path/to/dir/to/create
- Recursively create directories if necessaryscp -r <host>:</source/path> <host>:</destination/path>
- Recursively copy files & directories over sshprintenv
- List all environment variableslsof -i :<port>
- Find out which process is listening upon a port
a=${VAR:-20}
- will assign to a the value of VAR if VAR is set, otherwise it will assign it the default value 20 -- this can also be a result of an expression. This approach is technically called "Parameter Expansion".a=${1:-20}
- Same as above, but checking "positional arguments".[[ $b = 5 ]] && a="$c" || a="$d"
- If $b = 5, then a will get set to $c, otherwise it will get set to $d.
Reference: http://stackoverflow.com/questions/3953645/ternary-operator-in-bash
dpkg --list
- list out all packagesdpkg --list | less
- list out packages, in parts. let's you scroll through themdpkg --list | grep -i 'http'
- list out packages with http in the namesudo apt-get remove {packagename}
- basic uninstall, doesn't remove configuration files, doesn't remove full package “tree”, aka the other packages that are dependencies of that packagesudo apt-get --purge remove {packagename}
- basic uninstall that includes all config filessudo apt-get --purge autoremove {packagename}
- full uninstall that removes config files as well as all dependency packages
cat ~/.npmrc | awk -F'=' '{if($1=="//registry.custom.npme.io/:_authToken"){print $2}}'
- grab npm token for custom enterprise registry. -F allows you to specify custom delimeter, then using an if statement in the "program" we can check one match and print another.
find . -type d -exec sudo chmod 775 {} \;
- recursively update permissions of all directories starting from where you are. (Change d to f for files)sudo find / -name newrelic
- searches the entire server for files & directories that have the name newrelicls * | grep -v .gitignore | xargs rm -rf
- this will delete all files in your current directory other than the .gitignore file. Works well for cache directories.find <path> -type f -name "Icon*[^\.]" -exec rm -f {} \;
- Find files recursively and delete them. If you use a period for the<path>
it will begin searching in the current directoryfind <path> -type f | wc -l
- Count number of files in (recursive)find <path> -type d | wc -l
- Count number of directories/subdirectories in (recursive)find <path> -type l | wc -l
- Count number of symlinks in (recursive)find <path> -delete
- Delete everything in . Use a.
for the path to delete everything in your current working directory.
https://www.lifewire.com/uses-of-xargs-command-2201091
find ./ -type f -name "*.txt" -print | xargs -l200 -i cp -f {} ./backup
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
cat /etc/passwd
- list out all userscat /etc/group
- list out all groups and show what users are in those groupssudo usermod -a -G webteam chris
- add existing user to an existing groupsudo usermod -a -G sudo chris
- add existing user to sudo group
cat /var/log/apache2/example.com-access.log | awk '{print $9}' | sort | uniq -c | sort -nr
Get apache response code counts for your site from access log
/etc/init.d/<servicename> status
- check if a service is currently runningsudo update-rc.d service defaults
- set a service to start on defaulttop
- The top program provides a dynamic real-time view of a running system i.e. actual process activity. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.service --status-all
- shows the status of all installed services.cat /etc/*-release
- find out what version of Linux (distro) you are runningcat /proc/version
- see kernel version and gcc version used to build the same
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
- find public IP address for machine
curl -I
,curl --head
- Just retrieve headerscurl http://somurl.com/article/[1-25].jpg -O -s -f &> /dev/null &
- Download a range of urls
/var/log/messages
- General log messages/var/log/boot
- System boot log/var/log/debug
- Debugging log messages/var/log/auth.log
- User login and authentication logs/var/log/daemon.log
- Running services such as squid, ntpd and others log message to this file/var/log/dmesg
- Linux kernel ring buffer log/var/log/dpkg.log
- All binary package log includes package installation and other information/var/log/faillog
- User failed login log file/var/log/kern.log
- Kernel log file/var/log/lpr.log
- Printer log file/var/log/mail.*
- All mail server message log files/var/log/mysql.*
- MySQL server log file/var/log/user.log
- All userlevel logs/var/log/xorg.0.log
- X.org log file/var/log/apache2/*
- Apache web server log files directory/var/log/lighttpd/*
- Lighttpd web server log files directory/var/log/fsck/*
- fsck command log/var/log/apport.log
- Application crash report / log file
To view log files use tail
, more
, less
and/or grep
commands.
tail -f /var/log/apache2/error.log
- Stream the latest errors being logged by apache.tail -f /var/log/apport.log
more /var/log/xorg.0.log
cat /var/log/mysql.err
less /var/log/messages
grep -i fail /var/log/boot
<command> >/dev/null 2>&1
- Redirect stdout and errors to /dev/null<command> &>/dev/null
- Shortcut for abovefor i in {1..20}; do <command using ${i}>; done
- quick way to iterate through a sequence and iterate a command for each one
My Faves
Ctrl + s
– stops the output to the screen (for long running verbose command)Ctrl + q
– allow output to the screen (if previously stopped using command above)Ctrl + z
– suspend/stop the commandCtrl + u
– delete from cursor to the start of the command lineAlt + b
– move backward one word (or go to start of word the cursor is currently on)Alt + f
– move forward one word (or go to end of word the cursor is currently on)Alt + d
– delete to end of word starting at cursor (whole word if cursor is at the beginning of word)Ctrl + r
– search the history backwards
Command Editing Shortcuts
Ctrl + a
– go to the start of the command lineCtrl + e
– go to the end of the command lineCtrl + k
– delete from cursor to the end of the command lineCtrl + w
– delete from cursor to start of word (i.e. delete backwards one word)Ctrl + y
– paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursorCtrl + xx
– move between start of command line and current cursor position (and back again)Alt + c
– capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)Alt + u
– make uppercase from cursor to end of wordAlt + l
– make lowercase from cursor to end of wordAlt + t
– swap current word with previousCtrl + f
– move forward one characterCtrl + b
– move backward one characterCtrl + d
– delete character under the cursorCtrl + h
– delete character before the cursorCtrl + t
– swap character under cursor with the previous one
Command Recall Shortcuts
Ctrl + g
– escape from history searching modeCtrl + p
– previous command in history (i.e. walk back through the command history)Ctrl + n
– next command in history (i.e. walk forward through the command history)Alt + .
– use the last word of the previous command
Command Control Shortcuts
Ctrl + l
– clear the screenCtrl + c
– terminate the command
Bash Bang (!) Shortcuts
Bash also has some handy features that use the ! (bang) to allow you to do some funky stuff with bash commands.
!!
- run last command!blah
– run the most recent command that starts with 'blah' (e.g. !ls)!blah:p
– print out the command that !blah would run (also adds it as the latest command in the command history)!$
– the last word of the previous command (same asAlt + .
)!$:p
– print out the word that !`would substitute!*
– the previous command except for the last word (e.g. if you type 'find some_file.txt /', then !* would give you 'find some_file.txt')!*:p
– print out what !* would substitute