Linux Bash Cheat Sheet
31 Jul 2022 - Alejandro Piña
Basic Linux bash commands that are used day to day
Groups and Users
List all users and his primary groups
cat /etc/passwd
Shows user identifier, its groups and groups belongs to
id myuser
Show all groups
cat /etc/group
Shows who is a member of a group
getent group mygroup
Append, add or remove primary/sumplemetary groups
-aappend-Gworking with suplementary group-gworking with primary group
usermod -a -G mygroup1,mygroup2 mysuser
Permissions
Changes owner recursively
-Rrecursively:mygroupoptional group, the default is primary group
chown -R myuser:mygroup myfolder
Adds or remove permissions
-Rrecursivelyu=user,g=group,o=others anda=everyone+/-add or remove,w=write,r=read,x=execution
chmod -R g+w myfolder
Compress uncompress
Compress by tar file
-ccreate an archive-zcompress with gzip-vdisplay progress-fallow specify file name
tar -czvf mycompressfile.tar.gz /source/path
Uncompress by tar file
-xuncompress-vdisplay progress-fallow specify file name
tar -xvf myfcompressfile.tar.gz
Reads content in tar file
-tlist-fallow specify file name
tar -tf mycompressfile.tar.gz
ZIP files compression
Compress folder
-rrecursively
(cd target_folder && zip -r ../target_file.zip .)
Uncompress zip file
-ddirectory path
unzip compressfile.zip -d ./
Navigation
Shows directory structure
-Iexclude pattern (optional)-Pinclude pattern (optional)
tree -I 'bin|obj' -P '*.cs'
Find file recursively
find ./ -name "[filename.ext]"
Finds file pattern and delete
find . -name "myfile*" -type f -delete
Finds content in files recursively
grep -rnw 'target_folder' -e 'target_content'
Rename files recursively
find . -name "*.txt" | rename 's/.txt/.html/'
Networking
Shows listenting ports
sudo lsof -i -P -n | grep LISTEN