Basic Linux Commands


Display details about commands

1
man command

List contents of a directory

1
ls
1
ls -l
1
alias ll='ls -alF'
1
alias la='ls -A'
1
alias ls="ls --color=auto"

Path

1
pwd

Go to home directory

1
cd

Move a level up

1
cd ..

Return to the previous directory

1
cd -

Go to a specific directory

1
cd /path/to/directory

Create

Create a new directory

1
mkdir /path/to/newdirectory/

Create a new subdirectory

1
mkdir -p /path/to/newdirectory/subdirectory

Copy

Copy a file

1
cp /path/to/file.xxx /path/to/destination/newfile.xxx

Copy a directory

1
cp -r /path/to/directory/ /path/to/newdirectory/

In Linux, folders end with a slash / and files do not.

Move

Move a file to a new folder

1
mv /path/to/file.xxx /path/to/newdirectory/

Move a file to a new folder and rename it

1
mv /path/to/file.xxx /path/to/destination/newfile.xxx

Move a file to current directory

1
mv /path/to/file.xxx ./

Remove

Remove a file

1
rm /path/to/file.xxx

Remove an empty directory

1
rm -r /path/to/directory

Remove a directory and its contents

1
rm -rf /path/to/directory

Permissions

Change permissions for owner (u), group (g), and others (o), respectively, and (a) for all. You can call the above letters with operator (+, -, =) and permissions (r, w, x), which stand for read, write, and execute, respectively.

1
chmod ugo+r, o-wx /path/to/file.xxx

You can also use 3 numbers to set permissions. Each number is a sum of the following:

  • 4 for read permission
  • 2 for write permission
  • 1 for execute permission

The first number is for the owner, the second for the group, and the third for others.

1
chmod 744 /path/to/file.xxx

Execute

Execute a file

1
./file.xxx

Multiple Shell Sessions

screen

Create a new session with a name

1
screen -S session_name

List all sessions

1
screen -ls

Attach to a session

1
screen -r session_name

Detach from a session

1
screen -d session_name

tmux

Create a new session

1
tmux

Create a new session with a name

1
tmux new -s session_name

List all sessions

1
tmux ls

Attach to a session

1
tmux a -t session_name

Detach from a session

1
tmux detach

Kill a session

1
tmux kill-session -t session_name

Rename a session

1
tmux rename-session -t old_session_name new_session_name