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

  • -a append
  • -G working with suplementary group
  • -g working with primary group
usermod -a -G mygroup1,mygroup2 mysuser

Permissions

Changes owner recursively

  • -R recursively
  • :mygroup optional group, the default is primary group
chown -R myuser:mygroup myfolder

Adds or remove permissions

  • -R recursively
  • u=user, g=group, o=others and a=everyone
  • +/- add or remove, w=write, r=read, x=execution
chmod -R g+w myfolder

Compress uncompress

Compress by tar file

  • -c create an archive
  • -z compress with gzip
  • -v display progress
  • -f allow specify file name
tar -czvf mycompressfile.tar.gz /source/path

Uncompress by tar file

  • -x uncompress
  • -v display progress
  • -f allow specify file name
tar -xvf myfcompressfile.tar.gz

Reads content in tar file

  • -t list
  • -f allow specify file name
tar -tf mycompressfile.tar.gz

ZIP files compression

Compress folder

  • -r recursively
(cd target_folder && zip -r ../target_file.zip .)

Uncompress zip file

  • -d directory path
unzip compressfile.zip -d ./

Shows directory structure

  • -I exclude pattern (optional)
  • -P include 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