DEV Community

Josh
Josh

Posted on • Edited on

Beginner Linux Commands

Display Current User:

whoami
Enter fullscreen mode Exit fullscreen mode

Get Current user UID - User ID, GID - Group Primary ID, Groups - Groups you belong too.

id
Enter fullscreen mode Exit fullscreen mode

System Monitoring:
Install htop and run it.

sudo apt install htop
htop 
Enter fullscreen mode Exit fullscreen mode

htop example
htop displays your systems performance. At the bottom of htop shows how you can use htop such as : search, filter, kill, etc. To exit htop just press q.

Show your current directory:

pwd
Enter fullscreen mode Exit fullscreen mode

Show path to home directory:

~
Enter fullscreen mode Exit fullscreen mode

List directories and files in your current directory:

ls
Enter fullscreen mode Exit fullscreen mode

Create empty a file

touch file.txt
Enter fullscreen mode Exit fullscreen mode

Create file with text in it:

echo "Hello World" > file.txt
Enter fullscreen mode Exit fullscreen mode

Create a hidden file using the . :

echo "This file is hidden" > .cantsee
Enter fullscreen mode Exit fullscreen mode

Display all files even hidden ones:

ls -a
Enter fullscreen mode Exit fullscreen mode

Create a copy of file:

cp myfile.txt myfilecopy.txt
Enter fullscreen mode Exit fullscreen mode

Copy to another directory:

cp myfile.txt newdirectory/
Enter fullscreen mode Exit fullscreen mode

Moving files and changing name:

mv myfile.txt movedfile.txt
Enter fullscreen mode Exit fullscreen mode

Move to new dirctory:

mv myfile.txt somedirectory/
Enter fullscreen mode Exit fullscreen mode

Change name of directory:

mv somedirectory newnamedirectory
Enter fullscreen mode Exit fullscreen mode

Move and rename one time:

mv somedir/myfile.txt ./somefile.txt
Enter fullscreen mode Exit fullscreen mode

Remove a file:

rm file.txt
Enter fullscreen mode Exit fullscreen mode

Remove empty directory:

rmdir somedir
Enter fullscreen mode Exit fullscreen mode

Remove directory and all inside it:

rm -r somedir
Enter fullscreen mode Exit fullscreen mode

Remove with prompts to confirm removal:

rm -i file.txt
Enter fullscreen mode Exit fullscreen mode

Remove with recursive and force. Be careful with this command!

rm -rf somedir
Enter fullscreen mode Exit fullscreen mode

Read file contents with cat

cat file.txt
Enter fullscreen mode Exit fullscreen mode

Read file contents numbered

cat -n file.txt
Enter fullscreen mode Exit fullscreen mode

Print the beginning of a file:

head # shows the first 10 lines
head -n1 file.txt  # -n1 prints the first line
head -c1 file.txt # -c1 prints the first character
Enter fullscreen mode Exit fullscreen mode

print the last content of the file:

tail file.txt# shows last 10 lines
tail -n1 file.txt # -n1 prints the first line
Enter fullscreen mode Exit fullscreen mode

See difference between 2 files:

diff myfile1 myfile2

diff -r ~/Downloads ~/Documents  # Directory diff with -r for recursive
Enter fullscreen mode Exit fullscreen mode

Top comments (0)