Nhảy tới nội dung

Basic Commands (Beginner)

If you are starting with Linux, I recommend you should start with Command Lines. In this post, we'll talk about basic commands for beginners to survive in Linux world.

Let's think of Linux as a castle with many rooms.

pwd: know where you are

this first command will list your current room

$ pwd
/tmp/castle
ghi chú

Room (dir) mean directory in Linux

ls: look around

ls commands allow you to list all the other rooms and chests in your current room

ghi chú

Chest are files in Linux, they are things that have contents. For example images, documents, videos,...

Directories (dir) are things that contain files and other directories. Like rooms.

$ pwd
/tmp/castle

$ ls
chest1 chest2 room1 room2 room3

$ ls -a
# output the hidden dirs
# hidden dirs start with dot '.'
. .. .hidden-chest .secret-room chest1 chest2 room1 room2 room3

$ ls -al
# output in long format more detail owner and type file
total 72
drwxr-xr-x 6 stackops stackops 4096 Apr 7 03:21 .
drwxrwxrwt 14 root root 49152 Apr 7 03:21 ..
-rw-r--r-- 1 stackops stackops 0 Apr 7 03:01 .hidden-chest
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:47 .secret-room
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room3

$ ls -al /tmp/castle/
# ls the files and directories inside path /tmp/castle
# output should be the same as we are in that directory already
total 72
drwxr-xr-x 6 stackops stackops 4096 Apr 7 03:21 .
drwxrwxrwt 14 root root 49152 Apr 7 03:21 ..
-rw-r--r-- 1 stackops stackops 0 Apr 7 03:01 .hidden-chest
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:47 .secret-room
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room3
thông tin

In long format, the first character - indicates it's a file, `d`` is a directory

thông tin

Linux filesystem structures as a tree. The root is called / and goes down the way. So our example will look like this:

cd: change room

So far we know where we are and can determine what is around us. To change the room we can use cd (change dir) command

$ cd
# will return to your HOME path

$ pwd
/home/stackops


$ cd /tmp/castle/
# use absolute path

$ pwd
/tmp/castle

$ ls -l
total 12
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room3

$ cd room1
# use relative path

$ pwd
/tmp/castle/room1

File Viewing and Editing

cat: get the content

While ls output the contents of a room, cat used is for the chest

$ pwd
/tmp/castle

$ ls -l
total 16
-rw-r--r-- 1 stackops stackops 14 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 02:46 chest2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room3

$ cat chest1
secret-weapon
another-weapon

$ cat chest2
content from chest2

$ cat chest1 chest2
# we can combine multiple files
secret-weapon
another-weapon
content from chest2
warning

cat should only be applied with text files or similar ASCII files. You shouldn't cat other non-ASCII files such as images or video because it's in binary format and will flood your screen.

File Management

cp/mv: copy and move

if want to duplicate or move our chests to a new location, we can use cp or mv

$ pwd
/tmp/castle

$ cp chest1 new-chest1
# copy content of chest1 to new-chest1

$ ls -l
total 24
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 20 Apr 7 13:03 chest2
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:05 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:06 room3

$ cat new-chest1
secret-weapon
another-weapon

$ mv chest2 room3/chest2
# move chest2 to room3

$ ls -l
# we no longer see chest2
total 20
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:05 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3

$ ls -l room3/
# it's here in room3
total 4
-rw-r--r-- 1 stackops stackops 20 Apr 7 13:03 chest2
cảnh báo

cp and mv by default don't check for existing file. It may overwrite the content of your destination file.

mẹo

add these line to your ~/.bashrc to prevent overwriting

~/.bashrc
alias cp="cp -i"
alias mv="mv -i"
$ cp chest1 new-chest1
cp: overwrite 'new-chest1'? y/n

touch: create an empty chest

this is nothing but creating a new empty chest

$ pwd
/tmp/castle

$ touch empty-chest

$ ls -l
total 20
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3

You can see the new empty-chest is created with the size of 0.

mkdir: create a new room

this is similar to touch but applied to rooms

$ pwd
/tmp/castle

$ ls -l
total 20
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3

$ mkdir room4

$ ls -l
total 24
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:29 room4

rmdir: remove empty room

if your room is empty (no chests, no files), you can remove it with rmdir

$ pwd
/tmp/castle

$ ls -l
total 24
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:29 room4

$ ls -l room4/
total 0

$ rmdir room4

$ ls -l
total 20
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3

rm: remove room or chest

rm works both for removing a room or a chest

$ pwd
/tmp/castle

$ ls -l
total 20
-rw-r--r-- 1 stackops stackops 29 Apr 7 12:46 chest1
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3

$ rm chest1

$ ls -l
total 16
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room2
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3

$ rm -rf room2
# -r for recursive
# -f for force

$ ls -l
total 12
-rw-r--r-- 1 stackops stackops 0 Apr 7 13:26 empty-chest
-rw-r--r-- 1 stackops stackops 29 Apr 7 13:20 new-chest1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 02:46 room1
drwxr-xr-x 2 stackops stackops 4096 Apr 7 13:08 room3
cảnh báo

Like cp or mv, rm doesn't warn you before removing the files.

NEVER EVER USE rm -rf / or rm -rf /* which may destroy your current Linux machine.