Getting older, not necessarily wiser!
In the past several posts we have discussed the Linux Command Line and File System. We learned to look at directory content and move around the file system with ls and cd. In this post we will look at creating links, using a pipe, and viewing file content. We will be using the commands ln, cat, more, and less.
We will use then local user file structure from our last post. It is presented below for reference.
/ |_ home |_ user | |_ Documents | |_ Graphics | |_ Projects | |_ CodeProject1 | |_ ImageProject1 |_ OtherUser
As with previous commands, it is strongly recommend to review the associated man page on your system. In this case $ man ln. This will give you information specific to your system. In some cases commands can differ slightly depending on the distribution.
The ln command can be used to create either soft (symbolic) links or hard links. We will focus on the soft links, as I have found soft links generally more useful. It has been my experience that hard links are only useful for specific tasks, while soft links are much more general in their application.
The ln command is generally used as follows to create a soft link;
$ ln -s [file/directory] <link path and name>
If the -s is omitted, then ln will create a hard link. A file or directory to be linked is required. Optionally we can add a path and name for the link. If not added, a soft link will be created in the current directory. As always an example should help clarify this.
Lets say we have a file, LogoGraphic.png in /home/user/Projects/ImageProject1. We want to have access to it from the CodeProject1 directory without actually moving it. Nor do we want to type in the absolute or relative paths over and over again. To solve this problem we will create a soft link in CodePorject1 pointing to our graphic in ImageProject1.
We will cd into the Codeprojec1 directory so we do not need to add a path to our link, and can just give it a name. We will also use the relative path to the file.
$ ln -s ../ImageProject1/LogoGraphic.png LogoGraphic1Link
Now when we list then directory with the -l option, we will see the link file and where it points.
$ ls -l lrwxrwxrwx 1 rt rt 32 Jan 28 06:39 LogoGraphic1Link -> ../ImageProject1LogoGraphic.png
In real life situations you will probably want to use the absolute path. We used the relative path mainly because of limited line space of this post.
The first command we will use to look at file content is the cat command (did you read the man page?). The cat command will print the content of a file to the terminal. It is non destructive, meaning you can not change a file content with it.
Lets use it to see what is in our .bashrc file that we have mentioned in previous posts.
$ cat .bashrc
The content of the file will vary depending on your distro. But you should be able to see where the PS1 prompt variable is set. Next lets look at the passwd file in the etc directory.
$ cat /etc/passwd
This file contains all the users for your system. Most of them are system level users that you will never use (other than root on some occasions). At the bottom of the list you should see your normal user account. Back in the old days passwords were stored in this file, hence the name. However that was not very secure, so there is a different method in place now for passwords.
Another file we might want to look at is the group file.
$ cat /etc/group
In Linux a file has permissions for the user, a group, and everyone else. The group file will show you what groups you are in.
Note: I think we have come far enough that I can dedicate the next post to security and permissions on a Linux system. Since most of us are desktop users, and that means we are the system administrator and may have to deal with permissions and security. Unless you are going to allow multiple users, it is probably not something you will need to worry about to much.
One more example before we move on. There is a root directory on your system called proc. Within it is an entry called cpuinfo. Note the files within /proc are special and represent the current state of the kernal. If you are interested in your cpu, enter the following command.
$ cat /proc/cpuinfo
This should print to your terminal everything the kernel knows about your CPU. If you are feeling froggy, try grabbing the info from the /proc/meminfo location.
This brings us to standard in (stdin), standard out (stdout) and standard error (stderr). Commands have stdin and stdout. Most commands have a stderr also by default your keyboard is stdin and your display is stdout.
You may have noticed that in some cases information from cat scrolls off the top of the screen. Instead of printing to the screen (stdout), we can redirect the information to a file. To redirect stdout, we use “>”. For example, to redirect our cpuinfo output to a text file we would do this.
$ cat /proc/cpuinfo > mycpuinfo.txt
You can read more about stdin/stdout/stderr in this post.
Assuming you have been following along, and you have a text file called mycpuinfo.txt, you can read it with either the more or less command.
Both more and less allow you to review a file on screen one page at a time. You will want to read the man pages for both commands, and try them out to see which one you like better. Both are easy to use.
$ more cpuinofo.txt $ less cpuinfo.txt
One final option for this post, the pipe. A pipe uses the “|” character, and sends the stdout of one command to the stdin of another command.
Lets say you want to look at the meminfo in then /proc directory, but you do not want to clutter up your home directory with a text file. This is where a pipe comes in. We will pipe the stdout of cat to the stdin of lee in this example. Thus the meminfo information will be displayed in less without having to create and delete a text file.
$ cat /proc/meminfo | less
You can read more on pipes in this post.
In this post we have looked at links (ln) and a few commands for reading data in files (cat, more, less). We also touched a little bit on pipes and standard input/output.
In the next post we will be looking at permissions on your Linux system.
The Linux Command Line Part 5: ln, cat, more, less