Skip to content

Instantly share code, notes, and snippets.

@anderson-custodio
Last active August 19, 2019 11:44
Show Gist options
  • Save anderson-custodio/3f2452ad977c88787f6a548dd272b411 to your computer and use it in GitHub Desktop.
Save anderson-custodio/3f2452ad977c88787f6a548dd272b411 to your computer and use it in GitHub Desktop.

Linux useful commands

Print file with line number:

cat -n notes.sh

Write to file replacing all content:

echo "foo" > teste.txt

Write to file appending the content:

echo "foo" >> teste.txt

Remove empty folder

rmdir foo

Remove non empty folder

rm -r foo

Compress directory using zip

zip -r my_dir.zip my_dir

Show zip file content

unzip -l my_dir.zip

Uncompress content

unzip my_dir.zip

Compress using tar and gzip

tar -cz my_dir > work.tar.gz # with output redirect
tar -czf work.tar.gz my_dir/ # without output redirect

Compress using tar and bzip2

tar -cjf work.tar.bz2 workspace/

Uncompress tar file

tar -xz < work.tar.gz # with output redirect
tar -xzf work.tar.gz # without output redirect
tar -vxzf work.tar.gz # without output redirect and with verbose mode

Show current date formated

date "+%d/%m/%y"

Show file's first 10 lines

head foo.txt

Show file's first N lines

head -n 3 foo.txt

Show file's last 3 lines

tail -n 3 foo.txt

Show file allowing navigation with UP/DOWN keys

less foo.txt

VI - INSERT MODE

i inserts in the current column
a inserts in the next column
A inserts in the end of the line
x deletes current character
5x deletes next 5 characters
5dd deletes 5 lines

VI - NORMAL MODE - Navigation

G goes to last line
1G goes to first line
$ goes to line end
0 goes to line begin

VI - NORMAL MODE - Search

/word + Enter
n goes to next result
N goes to previous result

VI - VISUAL MODE - Copy/Paste

yy copy line
3yy copy 3 lines
p paste
2p paste 2 times

Processes

# Show all processes
ps -e

# Stop proccess
kill 1234

# Kill proccess
kill -9 1234

# Search proccess
ps -ef | grep firefox

# Kill all processes by name
killall -9 firefox

# Show proccess by user
top -u anderson

Jobs, bg, fg, pstree, &

# Show processess tree
pstree

# Run proccess in background (terminal unlocked)
firefox &

# Run proccess in foreground (terminal locked)
firefox

# Suspend proccess
ctrl + z

# Show jobs
jobs

# Send job to background (terminal unlocked)
bg 1

# Send job to foreground (terminal locked)
fg 1

sh e chmod

# Add execution permission to all users and group
chmod +x file.sh

# Remove execution permission
chmod -x file.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment