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

Show user identifier his groups and groups belongs to

id myuser

Show all groups

cat /etc/group

Show 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

Change owner recursively

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

Add 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

Read contents 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 ./

Show directory structure

  • -I exclude pattern (optional)
  • -P include pattern (optional)
tree -I 'bin|obj' -P '*.cs'

Find file recursively

find ./ -name "[filename.ext]"

Find file pattern and delete

find . -name "myfile*" -type f -delete

Find contents in files recursively

grep -rnw 'target_folder' -e 'target_content'

Networking

Show listenting ports

sudo lsof -i -P -n | grep LISTEN