Most Linux Server does not have a graphical user interface (GUI). Instead, we will use shell commands to interact with the system. This article will introduce some basic Linux commands.

Passing flags!

When a command syntax allows you to pass [flags], you can pass mutiple flags, NOT just one!

You can do:

ls -lah

Three flags: l, a, h

rm -rv

Two flags: r, v

1. ls command

The ls command prints the current directory's contents.

Basic syntax

ls [-flags] [name...]

Command without any flags

It will list all the files and directories (folders) by default.

# username @ yourmachinename in ~ [8:24:31] 
$ ls
lnmp-install.log  lnmp1.8  lnmp1.8.tar.gz  snap

ls + directory

You can use the ls command to list a specified directory. An example to list the /etc directory:

# username @ yourmachinename in ~ [8:24:31] 
$ ls /etc/
NetworkManager                 console-setup         environment  init.d           login.defs    
PackageKit                     cron.d                ethertypes   initramfs-tools  logrotate.conf
X11                            cron.daily            fonts        inputrc          logrotate.d   
adduser.conf                   cron.hourly           fstab        iproute2         lsb-release   
alternatives                   cron.monthly          fuse.conf    iptables         ltrace.conf   
apparmor                       cron.weekly           fwupd        iscsi            lvm           
apparmor.d                     crontab               gai.conf     issue            machine-id    
apport                         cryptsetup-initramfs  groff        issue.net        magic         
apt                            crypttab              group        kernel           magic.mime    
at.deny                        dbus-1                group-       kernel-img.conf  mailcap      

-l flag

ls -l will list things in a long format showing properties, timestamps, etc..

# username @ yourmachinename in ~ [8:24:31] 
$ ls -l
total 3092
-rw-r--r-- 1 root root 2975510 Aug 13 11:45 lnmp-install.log
drwxr-xr-x 7 root root    4096 Jan  6  2021 lnmp1.8
-rw-r--r-- 1 root root  173302 Jul 23 13:26 lnmp1.8.tar.gz
drwxr-xr-x 3 root root    4096 Aug 13 10:03 snap

-a flag

ls -a could list all files, including hidden (system) files.

# username @ yourmachinename in ~ [8:24:31] 
$ ls -a
.                        .mysql_history        .zcompdump
..                       .oh-my-zsh            .zcompdump-ubuntu-s-1vcpu-1gb-sfo3-01-5.8
.Xauthority              .pearrc               .zsh_history
.bash_history            .profile              .zshrc
.bashrc                  .shell.pre-oh-my-zsh  lnmp-install.log
.cache                   .ssh                  lnmp1.8
.cloud-locale-test.skip  .vim                  lnmp1.8.tar.gz
.cmake                   .viminfo              snap
.local                   .wget-hsts

2. cd command

The cd (change directory) command is for navigating directories.

You can use cd with both absolute and relative paths.

~ represents the current user's home directory; . represents the current directory; .. represents the parent directory;

Basic syntax

cd [dirName]

cd will return to user's home directory (Same as cd ~ command)

# username @ yourmachinename in /etc/network [8:56:20] 
$ cd
# username @ yourmachinename in ~ [8:56:28] 
$ 

cd ..

cd .. will change path to the parent directory

# username @ yourmachinename in ~ [8:40:25] 
$ cd ..
# username @ yourmachinename in / [8:53:22] 
$ 

3. mkdir command

The mkdir (make directory) allows you to create a directory

Basic syntax

mkdir [-flags] dirName

Create a new directory named runoob in the current directory

# username @ yourmachinename in ~ [8:56:28] 
$ mkdir runoob

-p flag

Some similar error messages will appear if you want to create mutiple folders without -p flag attached.

# username @ yourmachinename in ~ [8:56:28] 
$ mkdir test/inTestFolder
mkdir: cannot create directory ‘123/234’: No such file or directory

After you pass -p flag, you can

# username @ yourmachinename in ~ [8:56:28] 
$ mkdir -p test/inTestFolder

4. rm command

The rm command is for removing a file or directories.

There is no “recycle bin” – if you delete it – it’s gone!

Basic syntax

rm [-flags] name...

Remove a single file

# username @ yourmachinename in ~ [8:56:28] 
$ rm test.txt

-r flag

-r allow you remove directories. (for recursive)

# username @ yourmachinename in ~ [8:56:28] 
$ rm -r testfile

5. chown command

root privileges required to run this command

The chown (change owner) command can set a file ower or group.

Basic syntax

chown [-falgs] user[:group] file...

Set /var/run/httpd.pid file to root owner

# root @ yourmachinename in ~ [8:56:28] 
$ chown root /var/run/httpd.pid

Set file.txt file to spsp owner, family group

# root @ yourmachinename in ~ [8:56:28] 
$ chown spsp:family file.txt

-R flag

Set ownership recursively:

# username @ yourmachinename in ~ [8:56:28] 
$ chown -R spsp:family *

6. cp command

The cp (copy file) command can copy files and directories.

Linux cp(英文全拼:copy file)命令主要用于复制文件或目录。

Basic syntax

cp [-flags] [file] [target]

Copy test.txt file under usr directory to home/newtest directory

# username @ yourmachinename in ~ [8:56:28] 
$ cp usr/test.txt home/newtest

-r flag

-r flag is required when you copy a directory (folder).

Copy usr directory to home/newtest

# username @ yourmachinename in ~ [8:56:28] 
$ cp -r usr/ home/newtest

7. mv command

The mv command allows you move and/or rename files.

Basic syntax

mv [-flags] [file] [target]

Rename file in same location

# username @ yourmachinename in ~ [8:56:28] 
$ mv source_file.txt dest_file.txt

move file only

# username @ yourmachinename in ~ [8:56:28] 
$ mv source_file.txt dest_directory

Rename and move file

# username @ yourmachinename in ~ [8:56:28] 
$ mv source_file.txt dest_directory/target_file.txt

8. pwd command

The pwd command can print current director.

Basic syntax

pwd

Related Posts