DEV Community

Cover image for Must know basic Linux commands
Patrick
Patrick

Posted on • Updated on • Originally published at blog.thepatik.com

Must know basic Linux commands

Linux is an open-source operating system with a huge number of distributions, some of them:

  • Ubuntu
  • Linux Mint
  • CentOS
  • Fedora
  • Debian

Most of these operating systems have a graphical interface that provides a wealth of features, but the greatest power of Linux comes with the use of a command-line interface (CLI)

For this reason, it is useful to know some of the most common Linux CLI commands.

Commands for working with files

cd command

Many times, we can change the active directory we are currently working with. This is done with the cd command.

cd /home/user/folder1
# Current directory will be /home/user/folder1
Enter fullscreen mode Exit fullscreen mode

ls command

The ls command is used to display the contents of the current directory.

ls

# Output example
Desktop Documents Downloads Music Public
Temp .gitconfig

ls -R # list all the files in the sub-directories
ls -a # show hidden files
ls -al # list all info's about a file (size, owner ...)
Enter fullscreen mode Exit fullscreen mode

pwd command

The command tells us which is the currently active directory.

pwd # return the current directory
# output example
/home/user/folder1
Enter fullscreen mode Exit fullscreen mode

cat command

Perhaps the most commonly used command in Linux, as it allows you to create, combine files.

cat > file # create file with name file in current directory
cat file1 file2 > file3 # joins file1 and file2 and create output file3 in current directory
Enter fullscreen mode Exit fullscreen mode

cp command

cp command disables copying a file from the current directory to another.

cp document.txt /home/user/Documents # copy document.txt from current directory to folder called Documents
Enter fullscreen mode Exit fullscreen mode

mv command

mv command allows you to move files.

mv document.txt / home / username / Documents # move document.txt from current directory to folder called Documents
Enter fullscreen mode Exit fullscreen mode

You can also rename files with this command.

mv oldname.txt newname.txt # rename file from oldname.txt to newname.txt in the current directory
Enter fullscreen mode Exit fullscreen mode

mkdir command

This command creates new folders.

mkdir Archive / Documents # create folder called Documents from current directoy
mkdir -p Archive / 2021 / Documents # create folder 2021 between folders Archive and Documents
Enter fullscreen mode Exit fullscreen mode

rmdir andrm command

Commands can be used to delete folders.

With rmdir we can only delete empty folder.

With rm, you can delete the entire folder (check before use because it is not possible to restore the deletion).

rmdir NotusefulFolder # remove NotusefullFolder (it is empty) from the current directory
rm Folders / NotInUse # remove all files and folders in folder NotInUse from the current directory

rm -r NotUsefulFolder # command equal to rmdir
Enter fullscreen mode Exit fullscreen mode

touch command

The command allows you to create new files.

touch file.txt # create a file in the current directory
Enter fullscreen mode Exit fullscreen mode

find command

Used to search for a specific file.

find
Enter fullscreen mode Exit fullscreen mode

cat command

This command allows us to print the contents of a file.

cat
Enter fullscreen mode Exit fullscreen mode

head command

Display us the first 10 lines of the file.

head file.txt
Enter fullscreen mode Exit fullscreen mode

tree command

Return filesystem directory tree.

tree
Enter fullscreen mode Exit fullscreen mode

Setup commands

sudo command

Abbreviation for "SuperUser Do", with this command we can execute commands that require administrative authority.

sudo <SOME_ACTION> # In the next step you must input the password for the root/admin user
Enter fullscreen mode Exit fullscreen mode

df command

Shows us disk usage in KB and percent.

df # Return disk usage in KB
df -m # Return disk usage in MB
Enter fullscreen mode Exit fullscreen mode

kill command

The Kill command allows you to stop a specific process.

ps # return list of active processes

kill 11111 #kill process with PID 11111
Enter fullscreen mode Exit fullscreen mode

zip and unzip commands

They allow us to create compressed zip files and expand zip files.

ping command
The command is used in most cases when we want to check if a device is active on the network

ping hashnode.com
Enter fullscreen mode Exit fullscreen mode

hostname command

The command tells us the name of the device

hostname # return hostname of the device
# example output
server
Enter fullscreen mode Exit fullscreen mode

useradd andpasswd command

useradd allows us to create a new user. Passwd allows us to change or set a password.

useradd NewUserName # create new user
userdel UserName # delete user

passwd UserName # can set a password if doesn't exist or change if exists
Enter fullscreen mode Exit fullscreen mode

wget command

A very useful command to download files.

wget DownloadFileURL # download file in current directory
Enter fullscreen mode Exit fullscreen mode

free command

After giving us the amount of memory used.

free -h # return usage of memory - RAM (human-friendly numbers and units)
Enter fullscreen mode Exit fullscreen mode

shutdown andreboot command

Simple commands - the sessions already name what they learn.

shutdown # shutdown computer
reboot # reboot computer
Enter fullscreen mode Exit fullscreen mode

Maybe if you have read in detail and get to know the Windows terminal, then you have noticed some commands are the same.

Of course, there are even more of these commands, but I mentioned the most commonly used ones. If I forgot any, and you find it important to use in Linux, mention them in the comments 😊.

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

touch is not actually designed for creating new files at all. It merely updates the last access and/or modification date of the given file. The fact that it happens to create the file if it doesn't already exist is just a convenient side effect

Collapse
 
patik123 profile image
Patrick

Thanks.