Some Basic Linux Commands I Have Used During My Linux Journey – Part 8

As each day passes the more new linux commands I learn. A new command gives me some new power on the linux system, a power which I did not have before.  For example yesterday I learned how to locate manual pages by subject using the following syntax which is very easy to understand and use even for beginners.

man -k pwd

 Running the above command on my machine prints the following on the standard output.

lckpwdf (3) - get shadow password file entry
pwd (1) - print name of current/working directory
pwdx (1) - report current working directory of a process
ulckpwdf (3) - get shadow password file entry
unix_chkpwd (8) - Helper binary that verifies the password of the curren...

It finds all the manual pages that talk about the pwd command. If you want only the manual pages for the command which prints the name of the current working directory then you can make use of piping like shown below.

man -k pwd | grep "print"

Running the above command on my machine gives me the following output.

pwd (1)              - print name of current/working directory

The which command

which is very useful when you want to locate a command. When working as a developer you may use this command to find out version of development tools you are using. For example the following command prints on standard output the python interpreter which I am currently using.

which python

 The following is showed on my standard output.

/usr/bin/python

As you can see from the above output of the which command, the location of the executable file is printed out. According to the manual page the which command searches the PATH environment variable for paths matching the give filename arguments and if the matching occurs prints them to standard output.

The which command returns the following value, depending on what occurred:

  • All filenames were found, and all were executable.
  • One or more filenames were not found, or were not executable.
  • An invalid option was specified.

Lets take the following example. Run the command which is shown below on your console and see what is going to happen. I have to tell before you run the command that is not going to do anything bad.

which sh

It just gives you some useful information. When running the above command on my console the following is printed on standard output.

/bin/sh

What does the which sh command do? It locates the path of the file which would be run if the sh command were executed. Then it prints this path on the standard output.

Try to run the following command.

which ls

What do you get on your standard output? I get the following.

/bin/ls

What about the following command?

which pwd

Does it print the following on standard output.

/bin/pwd

Then try another command.

which cd

You should get the output which is shown below.

/bin/cd

As you can see from the above examples the binaries of linux builtin commands such as ls, pwd, cd etc are located inside the /bin directory which stands for binary files.

The find command

The find command is used to find files in a directory hierarchy. In other words this command searches and locates files on your linux system.

Try to run the following command inside the current directory you are working.

find

What do you see on the standard output once you have executed the above command? Maybe many files will be printed on your standard output depending on what the directory contains.

When used like shown above the find command will print a list of files inside the directory including all the files inside subdirectories that are part of this directory.

Then run the following command.

find . /home/your_user_name_here/Desktop

The above command is going to print all files and directories inside Desktop directory. You can also specify other directories for the searching to take place like shown below.

find . /home/your_user_name_here/Desktop /home/your_username_here /home/your_username_here/Desktop/some_directory_here

Now lets say that you want to find files or directories that have a specific text in their name. For example I want to print all the files and directories that contain the text ‘oltjano’ inside their name.

find /home/oltjano/ -name "oltjano"

Running the above command on my system prints the following on my console.

/home/oltjano/
/home/oltjano/.wine-browser/drive_c/users/oltjano
/home/oltjano/.config/teamviewer9/drive_c/users/oltjano
find: `/home/oltjano/.kde/share/apps/k3b': Permission denied./oltjanostudent.py
 ./oltjano.py
find: `/home/oltjano/.kde/share/apps/RecentDocuments': Permission denied
find: `/home/oltjano/.car-p': Permission denied
find: `/home/oltjano/Desktop/.cpanel': Permission denied
find: `/home/oltjano/Desktop/.cagefs': Permission denied

We can also make use of piping to filter output out. For example the following command is going to find all files and directories that have the text “oltjano” in their name and filters others out.

find | grep oltjano

I get the following output when running the above command.

./oltjanostudent.py
./oltjano.py

And to show you guys files and sub directories inside the current working directory I am using the ls command to list everything inside it and paste the output in here.

First let me run the pwd command to print the current working directory.

pwd

I get the following output.

/home/oltjano/Desktop/students

Then to list the files inside the above directory I use the ls command like shown below.

ls

The output which is shown below is printed to standard output after running the above command.

firststudent.py  oltjano.py  oltjanostudent.py

The find command has many options and there are many other examples I can show to you guys, but I am thinking to dedicate an entire article to it.

The stat command

The stat command is used to display file or filesystem status.The syntax for using this command is very simple. It is shown below.

stat [OPTION]... FILE...

For example running the following command gives me detailed information about the test.txt file.

stat test.txt

The output of the above command is shown below.

 File: `test.txt'
 Size: 211 Blocks: 24 IO Block: 4096 regular file
Device: 15h/21d Inode: 11845478 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ oltjano) Gid: ( 1000/ oltjano)
Access: 2015-07-07 13:49:41.132137281 +0200
Modify: 2015-07-03 11:39:04.147857766 +0200
Change: 2015-07-03 11:39:04.271857760 +0200
 Birth: -

You can also return the status of the first hard disk by using the following command.

stat -f /dev/sda

The above command display the following on standard output.

 File: "/dev/sda"
 ID: 0 Namelen: 255 Type: tmpfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 487969 Free: 487968 Available: 487968
Inodes: Total: 487969 Free: 487434

The ifconfig command

The ifconfig command is used to configure network interfaces on your system or view the current configuration. Try running the following command on your console.

ifconfig

What output do you get?

The edit command

The edit command is used top edit files. This command makes use of the edit text editing utility. Use it like shown below.

edit test.txt

The apt-get command

This is a command line tool for working with APT software packages. I use this utility on my Ubuntu machine to install, update and remove software.

For example the following command is going to install python on my machine.

sudo apt-get install python

And If I want to remove a specific package I do the following.

sudo apt-get remove package_name_here

You can also automatically remove no longer required packages using the following command.

apt-get autoremove

To search for a specific packages do the following.

apt-cache search package_name_here

You can install updates on your machine by using the followings.

Run the following command to fetch all the available updates for your specific system version.

sudo apt-get update

Then upgrade the current packages with the following command.

sudo apt-get upgrade

Finally install new updates.

sudo apt-get dist-upgrade

Conclusion

As you have seen so far there are some many linux commands out there waiting for you. In this tutorial you learned some basic information about ifconfig, learned how to find files using the find command, how to display filesystem status with the stat command, how to use the which command and you also took a look at some practical commands of the apt-get utility.

I am sure that if you use Ubuntu you now know that the apt-get install command is used to install a new package on your machine. Maybe you want to remove a package in the future so remember to use the apt-get remove command.

You might want to read our previous parts of this series.