Beginner Linux Commands
Categories:
This article gives you an introduction of all the commands you’ll need for your daily Linux usage, including file management, process management, etc. You can connect to your Linux server using a terminal, and then try out these commands.
Sudo
Sudo
is a very special command. On Linux, most users do not have root access by default. Without root access, you cann’t make system level changes, e.g., installing packages, creating users.
In order to perform these operations under these accounts, you’ll need to elevate privileges with sudo. The usage of sudo is extremely simple, just put sudo
before your target command. For example, if you want to install tmux on Linux, you cannot do apt install tmux
, instead, you do sudo apt install tmux
.
A user can only use sudo command if allowed in the sudoers file.
By default, sudo command will ask you for password, but the setup here will help you skip that step.
File Commands
File system commands are the most basic commands on Linux. Here is a list of them.
ls
ls
is a simple command, meaning list files. It just list files in the current directory. You can also list all the files with its attributes by doing ls -alh
.
cd
cd
tells your shell to change your current directory to the target one.
pwd
pwd
returns what the current directory is.
mkdir
mkdir
means make directory. It makes a directory in the current directory. By default, it only create one layer of directory, but you can do mkdir -p {target_dir}
to create multiple layers of directories.
cp
cp
means copy file. It copies file from one location to another one. The format of the command is
cp {src-file} {dst-file}
where {src-file}
and {dst-file}
are the source and target file locations.
mv
mv
means move file. You can use it to move file from one location to another.
mv {src-file} {dst-file}
Note that renaming file and moving file are the same thing.
rm
rm
means remove. You can use it to remove a file, or remove a director (and its contents in it) with rm -Rf {target-dir}
.
rmdir
rmdir
means remove directory. You can use it to remove a directory.
touch
touch
just creates an empty file in the current directory. The format of the command is
touch {target-file-name}
After the command, a file named as {target-file-name}
will be created.
find
find
finds files in the file system. You can use it to find files with specific name patterns
find . -iname {your-file-pattern-regex}
or use it to find files instead of directories.
find . -type f
grep
grep
searches content in files. For example, if you want to search all the lines including sweworld in a file, you can do:
grep sweworld {target-file-name}
grep
is a very powerful tool, use man grep
or grep -h
to check out details.
head
head
shows the first few lines of a file, e.g., the following command shows the first 10 lines in the target file.
head -n 10 {target-file-name}
tail
tail
shows the last few lines of a file, e.g., the following command shows the last 10 lines in the target file.
tail -n 10 {target-file-name}
One of the important usage of tail is to print the new content of the file as the file is being appended with new content, which is done with the following command
tail -f {target-file-name}
tar
tar
can be used to pack a directory of content into a single file.
tar -cvf {target-tar-file-name}.tar {target-dir-name}
For example, if your {target-dir-name}
is sweworld
, and {target-tar-file-name}
is sweworld.tar
, it’ll pack all the content in the sweworld
dir into a file called sweworld.tar
.
To unpack the .tar file, do
tar -xvf {target-tar-file-name}.tar
tar
is useful whenever you want to transfer files over network, because transferring one single file is a lot faster than transfering many files.
chmod
chmod
modifies the access of a file. The Linux file system is delicated designed and needs another article to understand.
chown
chown
changes the owner of a file.
zip & unzip
zip
and unzip
will compress/decompress a file.
The usage of zip
is:
zip {target-zipfile} file1 file2 file3 ...
The usage of unzip
is:
unzip {target-zipfile}
Disk Commands
df
df
presents the free disk space for each block device. For example, the following command shows the disk usage of all the local disks.
df -alh
du
du
displays the disk usage stats.
du -h ./*
For example, the above command lists the disk usage of all the directories and files in the current directory.
Process Commands
ps
ps
lists the current running process information, including PID, TTY, running time, the command used to launched the process.
kill
kill
is used to kill a process. kill
kills a process by sending a SIGTERM signal. If that won’t kill a process, use
kill -9 {process-id}
to send a SIGKILL signal, which is the strongest process killing signal.
top
top
lists all process in a terminal UI. A fancy version of top
is called htop
, which needs installation.
Network Commands
curl
curl
can be used to download data from a server. For example, you can use
curl https://sweworld.net
to download the main page of sweworld.
netstat
netstat
shows the network status. It is typically used to check what ports are open on the target machine.
ipconfig
ipconfig
shows the interface configurations. It is typically used to check the IP address of a machine.
ping
ping
checks if it can send ICMP packets to a target IP address. It is typically used to check the network connectivity between your server and another one.
User Commands
useradd & userdel
useradd
and userdel
are used to create and delete users.
Package Commands
apt
apt
is advanced package tool. It is used to install packages on your Linux. For example, you can use the following command to install tmux
sudo apt install tmux
Note that it is typically used with sudo, since it requires root access.
System Commands
uname
uname
displays information about your system, e.g.,
uname -a
hostname
hostname
shows the hostname of the machine.
systemctl
systemctl
is used to manage services.
reboot
reboot
command reboots your machine. If you want to reboot your machine right away, type in
sudo reboot now
shutdown
shutdown
command shutdown your machine. If you want to shutdown your machine right away, type in
sudo shutdown -h now
Other Commands
echo
echo
just prints out what it is given.
man
man
means manual. It presents you the manual of another tool, e.g.,
man grep
shows your how to use the command grep.