Important Things Every Linux Beginner Should Know
#Devops#devopszerotoherobatchIII
1.Important linux commands and uses
As a developer, you’ll need to be comfortable navigating around the Linux file system. You can use the pwd
(Print Working Directory) command to see where you are.
cd
From your current location, you can move anywhere else with the
cd
(Change Directory) command. For example:
$ cd ~/.ssh/
In the above example the tilde (~
) represents your user’s home directory. If you’re inside a sub-directory and want to go up a level, you can do so with ..
, for example:
$ cd ../Documents
Many Linux beginners don’t realize that you can also use the tab key to auto-complete file and directory names.
touch
To quickly create a file you can use the
touch
command.
$ touch hello.txt
cp
To copy a file or directory, use the
cp
command. In example below, the left-most file is the original file, and the right-most file is the copy to be created.
$ cp hello.txt file1.txt
mv
Perhaps you made a mistake when naming your copied file and want to rename it. You can do this with the
mv
(Move) command. You can also use themv
command to move a file from one directory to another.
The below invocation of mv
will rename the file hello.txt
to bonjour.txt
.
$ mv hello.txt file1.txt
rm
Let’s say the creation of
ciao.txt
was a mistake. We can delete it with therm
(Remove) command.
$ rm file1.txt
If we want to delete a directory and everything in it, you can pass the-rf
flag to rm
. -f
will remove the files without confirmation, and -r
will remove files inside the directory. Note: be careful with this command. Make sure you are not deleting something important.
$ rm -rf ~/Downloads
ls
Let’s hold up a second. The Linux command line doesn’t show you what’s inside a particular directory unless you ask it to. It’s tough to be productive if you can’t see what you’re working with. This is where the
ls
command comes in useful.
$ ls ~/Downloads
ls
(List Files) allows you to see the filenames of any files in the given directory. If the command is run without an argument it will default to the current directory, but you can also specify a path on the right-hand side, as seen in the above example. ls
is the simplest form of this command, listing only filenames. Often, you’ll want to see more information about the files you’re interested in. You may also want to see hidden files, which sometimes hold important configuration.
The ls -l
command allows you to see a more detailed view of each file. It’s so commonly used that ll
is an alias that does the same thing. If you want to see hidden files as well, use ll -a
or ls -la
.
The ‘long list’ (-l
) version of the ls
command will show you the following information about each file you are inspecting:
File owner
File group
File size
Modification time
Filename
With these few commands, you should be able to comfortably move around the Linux file system, and create, move and delete files.
- cat
The cat
or concatenate command writes files to the standard output (the text you see on your screen in your terminal).
$ cat hello.txt
grep
cat
is often the easiest way to quickly inspect the contents of a file. It becomes especially powerful when piped into grep
:
$ cat guest_list.txt | grep Lucy
‘Piping’, i.e. the |
character, allows you to chain commands together, using the output of the left-hand side command as input to the command on the right-hand side. It’s a useful technique that allows you to do complex output processing by combining simple commands together.
One of the most common uses of the pipe command is to grep
the result of the left-hand side command. grep
, a catchy acronym for the not-so-catchy name Global Regular Expressions Print, is a simple utility that searches input for a line that matches the given pattern, in this case, a line containing the word ‘Lucy’.
Another very common use of cat
and grep
together is searching for a specific event in a large log file, i.e. /var/log/messages
.
$ cat /var/log/messages | grep '500 Internal Server Error'
grep
can be used for searching any kind of output, not just file contents. Many Linux commands output dozens of lines packed with information. For example, if your Linux machine is running over a dozen Docker containers, you can use grep
to zero-in on only the container you are interested in:
$ docker ps | grep my-awesome-container
2. File permissions and ownership
Every file and directory in the Linux file system has permissions and an owner. Permissions are who is allowed to do what with the file. To see the permissions on a file, use the command ls -l <filename>
. You’ll see something like this in the left-most column:
-rw-r--r--
This is a little hard to read, so let’s break it down in the example below:
..own grp oth
-|---|---|---
The dash on the far left will be replaced with a d
if the file is a directory. The next three groups of three dashes represent permissions for the owner of the file, the group of the file, and all others. The ‘owner’ of a file is the user who initially created it, though ownership can be changed (more on that shortly). The ‘group’ that owns a file will be the group that its owner belongs to, though this can also be changed. The permissions for ‘others’ apply to any user who is not the owner of the file and not in the group that owns the file. One exception is the ‘root’ user, which has full access to every file on the system.
Here is an example of a file where the owner has full permissions but nobody else can read, write or execute the file:
-rwx------
You may occasionally get a ‘Permission denied’ or ‘Username is not in the sudoers file’ error when trying to do something with a file or directory. This generally means your user does not have the correct permissions for what you are trying to do. You will need to change to a user who does, for example:
$ su sudo
To re-run your previous command as root, you can use sudo !!
, where the two exclamation marks will be replaced with your previous command.
You will occasionally need to change the permissions on a file:
$ chmod u=rwx,g=rx,o=r hello.txt
In the above example, we set read, write and execute permissions for the user, read and execute permissions for the group, and read permissions for other users.
If you’re game to learn it, there’s an even simpler shorthand for setting permissions:
$ chmod 766 hello.txt
The 7
represents owner permissions, the 6
represents group permissions, and the last 6
represents permissions for the group. But where do these numbers come from?
Each permission is represented by a digit. The permissions for each user type are added together to form the final number.
4 is “read”,
2 is “write”,
1 is “execute”
0 is “no permissions”
So, 7 represents 4 (read) + 2 (write) + 1 (execute). 6 represents 4 (read) + 2 (write), and so on.
You’ll need read permissions to inspect the contents of a file, write permissions to make changes to the file, and execute permissions to run scripts or executables.
You can change the owner and group of a file with the chmod
command. For example, let’s say you have a file with the following permissions:
drwxr-xr-x 32 root root 4096 16 Jul 17:48 directory1
You decide that you want your user account to be the owner of the file, and its group to be your group. As root, you can run the following command to change the file’s owner and group:
$ chown <your_user>:<your_group> hello.txt
When you run ls -l
on the file, you’ll see that its owner and group have changed:
drwxr-xr-x 32 your_user your_group 4096 16 Jul 17:48 directory1
vi
Vi is an ancient, powerful text editor that is installed on all Linux machines by default. First released in 1978, it has since spawned a more feature rich variant called Vim. Though it’s an old tool, many programmers swear by Vim. It’s older and lighter-weight variant Vi is the text editor most likely to be installed on any machine you may need to SSH on to. For this reason, having a basic understanding of how to use Vi can help you to quickly edit and manipulate the contents of files on almost any machine without leaving the comfort of your terminal.
Hope you’ve enjoyed learning more about Linux. Unlike many tools, operating systems and frameworks you’ll encounter in your programming career, Linux is relatively stable. Throughout changes and new versions, the fundamentals remain basically the same, and have done so for a while. The knowledge you’ve gained is likely to last you a long time.