Bash usage examples
Files routines
Display part of the file
$ cat filename | less $ tail -200 filename $ head -200 filename $ head -n 1000 filename | tail # display to stdout $ echo SOMETHING
Merge all files in folder to one
$ cat folder_path/* > new_file
Copy content of some text file
$ cat file_path | xclip -sel clip # ubuntu $ pbcopy < file_path # mac
Encoding:
# get encoding of document $ enca filename # change encoding of doc $ enconv filename
Network routines
Create an ssh key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/key_name cat ~/.ssh/key_name.pub | xclip -sel clip # ubuntu pbcopy < ~/.ssh/id_rsa.pub # mac
Get list of all listened ports
$ sudo netstat -peanut
One-liners
Show running process by name with headers
$ ps aux | egrep "required_name|PID"
Execute output of some command(assume it in text file)
$ $(cat filename)
Check memory every 1 second
$ watch -n 1 free -m
Get the location of the executable link
$ readlink -f /usr/bin/java
Get dependencies
$ readelf -d some_exe
Find location of installed files
$ dpkg --listfiles libqt4-dev
Get CUDA version
$ dpkg -l | grep cuda
Remove range of folders starts with numbers
$ rm -r output/logs/{17..26}*
Send stdout to file and display it to the bash at the same time
$ ./some_file.sh | tee -a logs.txt
Handle docker containers
# all runing| second line| container name | stop container by name $ docker ps | sed -n 2p | awk '{print $NF}' | xargs docker stop # stop all running containers $ docker ps | awk '{print $NF}' | tail -n +2 | xargs docker stop
Rename some folders by pattern
# find required folders | split folder by '/' or '_' | # based on split generate new name | call `mv` command with `system ()` flag # note that splaces added in quotes $ find . -type d -name "celeb*" | awk -F '/|_' '{system ("mv " $0 " " $2"_mobile_"$3)}'