Linux is an operating system just like Windows, iOS, and Mac OS. The duty of the any operating system is managing the communication between software and your hardware. If you are looking to get started with Linux this article specifically for you. In this tutorial , Linux Beginner’s guide, we’re going to discuss about 10 factors Linux user should know. Let’s get started.
1.Navigating the file system
If you are a developer, knowing how to navigate around the Linux file system is important. After open a terminal multiplexer like Terminator, you’ll be in the file system. Using the pwd (Print Working Directory) command, you can know where you are at now. You can move anywhere else from your current location using the cd (Change Directory) command. Look at below example.
$ cd ~/.ssh/
The tilde (~) is used to represent your user’s home directory. If you’re in inside a sub-directory and if you want to go up a level, you can use .. instead as below example.
$ cd ../Documents
Linux gives you chance to auto-complete file and directory names using tab key. If you are a beginner, it will be helpful of knowing this.
Using the touch command, you can create files quickly.
$ touch hello.txt
Using the cp command, you can copy a file or directory. Look at below example. In these example, the left file is the original file, and the right file is the copy to be created.
$ cp hello.txt ciao.txt
If you have done mistake while naming your copied file, you can rename it with the mv (Move) command. you can use command to move a file from one directory to another also. Look at below example and the way mv command was used. This command will rename the file hello.txt into bonjour.txt.
$ mv hello.txt bonjour.txt
rm (Remove) command
You can delete files that you created using the rm (Remove) command.
$ rm ciao.txt
If we wanna delete a directory and also everything in it, pass the-rf flag to rm. -f will remove files inside the directory and remove the files without confirmation also. But when you using this command make sure not to delete anything important.
$ rm -rf ~/Downloads
If you are not ask, the Linux command line won’t show you what’s inside a particular directory. Using is (List Files) command you can see the filenames of any files in the given directory.
$ ls ~/Downloads
If the command would run without an argument, it would default to the current directory. You can specify a path on the right-hand side in the command.
If you wanna see more information about the files your interest files, use the ls -l command or ls- ll command. If you want to see hidden files, use ll -a or ls -la commands. If you wanna know following information, use the ‘long list’ (-l) version of the ls command.
- File owner
- File group
- File size
- Modification time
- Filename
You will be able to move around the Linux file system comfortably using above commands.
2.cat, grep, and the wonders of piping
The cat command writes files to the standard output. It is the easiest way to inspect the contents of a file.
$ cat hello.txt
When it piped into grep, it’ll be extra powerful. This is a useful technique. You can do complex output processing by combining simple commands together using this technique.
$ cat guest_list.txt | grep Lucy
grep the result of the left-hand side command is one of the common usage of pip command. grep is a catchy acronym for the not-so-catchy name Global Regular Expressions Print. It is a simple utility that searches input for a line that matches the specified pattern, in above example, a line containing the word “Lucy”.
You can use cat and grep commands together to search a specific event in a large log file also .
$ cat /var/log/messages | grep '500 Internal Server Error'
You can search not only file content but also any kind of output using grep command. Many Linux commands output many lines that packed with information. As an example, if your machine is running over a dozen Docker containers, you can use grep to zero-in on only the container that you interestes in as below example.
$ docker ps | grep my-awesome-container
You can use redirection (>) to save the output of any command.
$ echo "Linux was created by Linus Torvalds" > bio.txt
This command will create a new file. If you wanna overwrite the contents of an existing file, use >> command instead of >.
3.find
If you are wanna find a file in some directory and there are many sub directories inside it, you can use find command. You can walk a file hierarchy and search it on several different dimensions using this command also. You can see them all by typing man find into your terminal.
$ find . -name CS101
Above example uses the -name flag to search for filenames that including the character sequence CS101.
4.File permissions and ownership
Every file and directory in Linux has permission and an owner. Permissions means who is allowed to do what with those directories and files. If you wanna see the permissions on a file, use the command ls -l <filename> . You’ll see something like below example.
-rw-r--r--
Let’s break in into like below example, because it’s hard to read.
..own grp oth
-|---|---|---
If the file is a directory then the dash on the far left would be replaced by a d. The next three groups with three dashes represent permissions for the group of the file, owner of the file and others. The owner of the file is the person who created it. The group of that file means the group that the creator belongs to. You can change the ownership and the group. The permission for others means, the people who have permission but they don’t belong to owner’s group or they are not owners.
Look at the below example. It is a file that owner has permission but anyone else cannot read it.
-rwx------
When you are going to do something with file or directory, you might get errors like “user name not in the sudoers file” or ” permission denied”. This errors mean you don’t have permissions for what you tried to do. Therefore you need to change for a user who has permission.
$ su sudo
You can use sudo !! command to run your previous command. You needs to replace previous command with exclamation marks also .
$ chmod u=rwx,g=rx,o=r hello.txt
Sometimes you might need to change the permissions on a file. Look at below example, in there we have given read, write and execution permission for user, read and execution permission for group and read permission for others.
There is another short way to setting permission.
$ chmod 766 hello.txt
numbers represent permissions
In above example, the number 7 represents owner permission, number 6 represents group permission and last number 6 represents permission for others. You might be curious where these numbers came from. Each permission has been represented by a digit. The permission for each user type has been added together to from the final number.
- 4 – read
- 2 – write
- 1 – execute
- O – no permission
7 represents 4+2+1 (read+ write + execute), while 6 represent 6+2 (read + write).
If you wanna inspect the content of the file, you need to have read permission. and also if you wanna make changes, you need to get write permission. You can run scripts or executables, by having execute permission.
Using chmod command, you can change the file’s owner and group. For example, if you have a file with following permissions,
drwxr-xr-x 32 root root 4096 16 Jul 17:48 cowsay.sh
You want to make your account as the owner of the file and your group as the group of the file, follow below command to make those changes.
$ chown <your_user>:<your_group> hello.txt
You can change the owner and group by running Is-I command.
drwxr-xr-x 32 your_user your_group 4096 16 Jul 17:48 cowsay.sh
5.reverse-i-search
Using reverse-i-search command, you can search back through your command history and you can re run a previous command. Hitting the up arrow allows you to cycle through command history also. But is you wanna go back previous command that you enter few minutes ago and if you have entered many commands since then, you can use reverse-i-search. To run reverse-i-search, type ctrl + r. Then you can type characters in your target command. If there are multiple matches you can hit ctrl + r again and cycle through them.
6.Watching, Tailing and Following
Sometimes you might want to re run a command and check the changes in the output. Then, you can run free command in such situations. For example, if you want to see the rate that memory usage of your computer is changing over time, you can run below command.
$ watch -n 5 free -m
This free command will show you memory usage in megabytes, in every 5 seconds.
If you want to see the latest changes to a file, you can use below command.
$ tail /var/log/messages
tail command will print the last ten lines of a given file. You can change the number of lines printed using -n flag. When the new lines are written to the file, the output won’t update. Because it prints the file at the time of the command was run. But you can fix this by adding -f flag like below example.
$ tail -f -n 100 /var/log/messages
Above command will show a live updated output of the last 10 lines.
7.man pages and getting help
Linux commands have optional parameters which define its behavior. They are prefixed with the dash ( -I ). Man pages comes here in order to help you, because it’s hard to remember all the options you can pass to a command. You can get access to a description of what the command does, list of possible options and what they do by typing man <command>. But this is not the only usage of man.
8.Checking and monitoring system resources usage.
Shortage of system resources like memory, CPU, disk space caused many operating system problems. Linux provides some tools for help to diagnose these issues. You can get information about all the processes running on your computer like memory consumption, CPU utilization, using top command.
You can see the current memory usage on your computer using free command also . Its helpful to check before do something whether you have got enough memory or not. You can see disk usage on your computer in a human readable format by using df-h command.
9.Managing processes
You can see all the processes that running in your computer using ps command.
$ ps aux
In above command, aux says ps to show processes owned by all users. You can use this as a diagnostic tool. You can use it when process running when it shouldn’t be and when process consuming many resources.
This command has many outputs. It will pair with grep when you wanna see information for a single process.
$ ps aux | grep ruby
When you want to kill the offending process, follow below command. Then you’ll be able to kill them by their name rather than their process ID.
$ pkill -9 Slack
You can kill every instance of a process by using killall command. For example, if you type killall ruby as your command, all the ruby processes will be killed. But you need to careful when using these commands and to kill the right processes.
10.vi
Vi is a ancient text editor. It is installed in all Linux machines by default. It was released in 1978. Even it was old, it is still used. Therefore you need to have some knowledge of how to use vi. It will be important in editing and manipulating the contents of file on any machine. If it’s take years to be a vi/vim expert, having little bit knowledge will be important.
Final thoughts
If you’re a new to this subject, Linux Beginner’s Guide and it’s tips will be useful to you. If you are interesting to learn more, use below books also.
- How Linux Works by Brian Ward
- The Linux Command Line by William E. Shotts
- Linux For Beginners by Jason Cannon
- Linux Command Line and Shell Scripting Bible by Richard Blum & Christine Bresnahan