Important basic Linux commands for absolute beginners - Part 1

So, you’re finally here! Finally taking that first step into learning Linux. And what other better way to learn Linux than to experiment with some basic commands that even Linux Administrators still use on a daily basis. Today I will be sharing with you a series of posts of Linux commands that I still use daily without even realizing it. We won’t go into anything too complex like awk or sed but more easier commands regarding file creation, editing, navigation and some others. Now in no way is the list in order of importance, we are just going over a list. Linux commands are like puzzle pieces, they are available and you have to figure out how to use them with one another.

Now, originally this post was going to be a singular list of important commands but that list turned from 5, 10, then 15 and realized there’s just too many important commands to cover. Instead, I’m going to make a series of commands beginners should know about Linux!

We will be looking at the following commands:

  1. cd
  2. cp
  3. mv
  4. ls
  5. chown

New to Linux?

If you are here and are actually new to Linux, maybe stop by and pick up a book to get started learning Linux!

1. cd

The cd command is used for navigating across the file system tree. If you need to move to a directory then quickly back to the previous, this is achievable with the cd command.

Lets say you want to navigate to your home directory. You can do this by adding a tilde after the cd command.

[linuxman@LINUXMAN-FED etc]$ cd ~
[linuxman@LINUXMAN-FED ~]$

Likewise you can also simply type in cd and it will have the exact same effect.

[linuxman@LINUXMAN-FED etc]$ cd
[linuxman@LINUXMAN-FED ~]$

What if you want to revert back to the previous directory you were at but it is too long to type out again?

We can overcome this by adding a hyphen after cd. Doing so, the previous directory will be displayed and you will be taken back. Running the same command again will take you back and fourth. So if you are for whatever reason needing to switch back and fourth from a directory, this is the command you need.

[linuxman@LINUXMAN-FED ~]$ cd /usr/share/doc/apache-commons-cli/
[linuxman@LINUXMAN-FED apache-commons-cli]$ cd
[linuxman@LINUXMAN-FED ~]$ cd -
/usr/share/doc/apache-commons-cli
[linuxman@LINUXMAN-FED apache-commons-cli]$

Now how we change directories exactly? Now that we got some tricks out of the way, we can be straight forward. The syntax for navigating into another directory is simply cd <path>.

So if you want to navigate to the /etc directory to edit some config files. You would run:

cd /etc

Now what if you want to just go up by one or 2 directories? We can achieve this by using .. as the path.

[linuxman@LINUXMAN-FED ssh]$ cd ..
[linuxman@LINUXMAN-FED etc]$

Here we moved from the ssh directory back up to the parent /etc directory.

2. cp

The cp command is super essential when it comes to modifying files especially when configuring a daemon. Either you need to make a backup of a file before modifying or you need to make a copy of that file elsewhere.

The basic syntax for the cp command is cp <source> <destination>.

Lets say I have a file called “test” and i want to copy it over to a sub-directory called “other”. The command to make a copy of “test” would look like this:

cp test other/test

One thing to note is that if you want the destination path of the file to have the same name as the original, you do not have to specify the destination file name. Only if you want the destination file name to be different is when you would specify the destination file name. So using the previous example, if I want to keep the original name it would look like this:

cp test other/

Now what if you have a lot of files with the same file ending? Maybe you have a ton of .log files in a directory and want to copy them in another directory to archive them or clear the clutter. This is where we introduce the use of “globs”.

So in our scenario, we have our archive directory called “archive” and we have our current directory full of log files. We can copy them all at once using the * glob. This glob is used to match all occurrences of the pattern.

cp *.log archive/

What if you want to copy an entire directory and its contents? For this we simply need to add the recursive switch in our command like so. Make sure that when you specify the destination directory, you add the trailing / at the end or it will think you want to replace the destination directory with the one being copied.

cp -R dir1 dest-dir/

There may even be times when you want to copy only specific files from one directory to the next. To do this, we specify the files we want to copy over inside curly brackets separated by commas.

cp {file1,file2,file3} /dest/dir/
3. mv

Similar to the cp command, the mv command moves files/directories instead of copying them. The same syntax exists mv <source> <destination>.

One thing to note is when it comes to moving directories with the mv command, you don’t have to specify a flag for recursive. Simply adding a trailing / at the end will tell the command it is a directory.

mv file1 /dest/file1
mv dir1/ /dest/dir1

Both of the above examples for a file and directory use the exact same syntax and produce the same results without the need for a recursive flag.

Likewise with globs, you can move the contents of a directory to another. The * glob will tell the mv command to move everything in that directory using that pattern.

mc dir1/*.log archive/

Oftentimes, you can use the mv command to simply rename files or directories. It follows the exact same syntax but on the destination parameter, you type in the new name of the file or directory.

mv oldfilename newfilename
- or -
mv olddirname/ newdirname/
4. ls

You might have already been asking yourself, “how exactly do I see the contents of the directory I am in?”. The ls command is what you use for seeing files, directories, hard/soft links and more file attributes. Depending on what you need, it can also be extremely useful in scripts.

The basic usage of the ls command is to run ls alone and it will output in columns all of the files in that directory.

[linuxman@LINUXMAN-FED ssh]$ ls
moduli      ssh_config.d  sshd_config.d       ssh_host_ecdsa_key.pub  ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
ssh_config  sshd_config   ssh_host_ecdsa_key  ssh_host_ed25519_key    ssh_host_rsa_key
[linuxman@LINUXMAN-FED ssh]$

The default output will list each file in order from left to right. Maybe you want to list all the files in 1 single column. To do this we use the -1 flag.

[linuxman@LINUXMAN-FED ssh]$ ls -1
moduli
ssh_config
ssh_config.d
sshd_config
sshd_config.d
ssh_host_ecdsa_key
ssh_host_ecdsa_key.pub
ssh_host_ed25519_key
ssh_host_ed25519_key.pub
ssh_host_rsa_key
ssh_host_rsa_key.pub
[linuxman@LINUXMAN-FED ssh]$

By now you should know that the way most Linux file systems hide files is by prefixing the file name with a period (.). If you are needing to list these hidden files, you use the -a flag.

[linuxman@LINUXMAN-FED ssh]$ ls -a
.       ssh_config    sshd_config.d           ssh_host_ed25519_key      ssh_host_rsa_key.pub
..      ssh_config.d  ssh_host_ecdsa_key      ssh_host_ed25519_key.pub
moduli  sshd_config   ssh_host_ecdsa_key.pub  ssh_host_rsa_key
[linuxman@LINUXMAN-FED ssh]$

Notice how we have . and ... These are not hidden files even though the ls command will output them. The single period is the current directory and the double period is the parent directory.

Finally for the most powerful, informative and personal favorite. Using the -l flag to list all files in long form. The long form flag shows you file type, permissions, number of links, user ownership, group ownership, size, date and file/directory name.

[linuxman@LINUXMAN-FED ssh]$ ls -l
total 556
-rw-r--r--. 1 root root     525809 Jun 10  2020 moduli
-rw-r--r--. 1 root root       1874 Jun 10  2020 ssh_config
drwxr-xr-x. 2 root root       4096 Jun 26  2020 ssh_config.d
-rw-------  1 root root       3661 Aug 28  2020 sshd_config
drwx------. 2 root root       4096 Jun 26  2020 sshd_config.d
-rw-r-----  1 root ssh_keys    480 Aug 19  2020 ssh_host_ecdsa_key
-rw-r--r--  1 root root        162 Aug 19  2020 ssh_host_ecdsa_key.pub
-rw-r-----  1 root ssh_keys    387 Aug 19  2020 ssh_host_ed25519_key
-rw-r--r--  1 root root         82 Aug 19  2020 ssh_host_ed25519_key.pub
-rw-r-----  1 root ssh_keys   2578 Aug 19  2020 ssh_host_rsa_key
-rw-r--r--  1 root root        554 Aug 19  2020 ssh_host_rsa_key.pub
[linuxman@LINUXMAN-FED ssh]$
5. chown

There will be times where you are going to need to take ownership or change ownership of files and directories. We can achieve this using the chown command.

Now as we learned before, we can check the owner and group of the files using the ls command. Lets say you have some files and you need to change the ownership from root to www-data (ubuntu/debian) or apache (fedora/centos).

chown www-data /var/www/index.html # Ubuntu/Debian
- or -
chown apache /var/www/index.html # Fedora/CentOS

Take note that when using the chown command, if you only specify one parameter, it will default to changing the user only. If the parameter passed is not a user, it will give you an error.

To specify the owner and the group, you write the parameters in the following syntax <owner>:<group>.

chown linuxman:admin /home/linuxman

If you want to apply the chown rules recursively to all sub-directories and files, you add the -R flag.

chown linuxman:admin -R /home/linuxman
Conclusion

This concludes the end of the first part of the new series of Linux commands for beginners. As always, we are only going over very basic usage of the commands mentioned. If you want to know more extensive usage of the commands, use the --help flag or you can reference the man pages.

Stay tuned for the next part!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *