Basic Linux Commands

This page lists basic Linux commands that are useful for new users.

1 Where am I?

Show the current directory:

pwd

2 List files

ls

Show details:

ls -lh

Show hidden files:

ls -la

3 Change directory

Go to a folder:

cd folder_name

Go back to your home directory:

cd

Go up one level:

cd ..

4 Create folders

mkdir project

Create nested folders:

mkdir -p project/data/raw

5 Copy files

Copy one file:

cp file.txt backup.txt

Copy a folder:

cp -r project project_backup

6 Move or rename files

Rename a file:

mv old_name.txt new_name.txt

Move a file into a folder:

mv file.txt project/

7 Delete files

Delete a file:

rm file.txt

Delete a folder and everything inside it:

rm -r folder_name
Warning

Be careful with rm. Files deleted from the command line may not be recoverable.

8 View text files

Show a whole file:

cat file.txt

View a long file page by page:

less file.txt

Show the first lines:

head file.txt

Show the last lines:

tail file.txt

Watch a file as it updates:

tail -f output.log

9 Edit text files

Use nano for simple editing:

nano script.R

Save with Ctrl + O, then press Enter. Exit with Ctrl + X.

10 Search inside files

Search for text in a file:

grep "error" output.log

Search recursively in a folder:

grep -r "parameter" .

11 Find files

Find files by name:

find . -name "*.R"

Find large files:

find . -type f -size +1G

12 Check disk space

df -h

Check the size of a folder:

du -sh folder_name

13 Check memory

free -h

14 Check running processes

top

or, if installed:

htop

15 Stop a running command

Press:

Ctrl + C

16 Command history

Show previous commands:

history

Search command history:

Ctrl + R

17 Useful shortcuts

Shortcut Meaning
Tab autocomplete file or command name
Ctrl + C stop current command
Ctrl + L clear screen
Ctrl + A move to start of line
Ctrl + E move to end of line