Search

Linux Tips

Tip 1: Change to previous directory



You can use the option "-" with cd to go back to your previous working directory instead of typing the full path. For example after executing the following commands



If we want to go back to the hello1 one folder we only need to pass the option "-" to cd



It will automatically change the present working directory to the previous one,which is hello1 in the example.

Tip 2: Change font color of the terminal



The font color by default is set to white on most linux terminals , if you want to try a different color you can use the command tput.



The above command should set the color to yellow. To clear the color and get back to the default color run the command

Tip 3: Mathematical Expressions on the teminal



If you want to execute simple mathematical expressions on the terminal using bash, you can do it by putting the expression between $(( )). For example

Tip 4: Create a csv file of directory contents



If we want to create a csv file having all the files in a folder you can use the option "-m" with the ls command as shown below If you open the files.csv, you should see all the filenames separated by commas.

Tip 5: Command to create an empty text file



If you want to just create an empty text file with out using any editor, you can use the command touch.

This will create an empty text file named temp.

Tip 6: To list only the latest edited file



If you want to view only the name of the file that was edited latest in the current directory, you can use the following commands



The output indicates "xyz" is the file that was edited latest. Hit "q" to quit or enter to view the file edited next.

Tip 7: Run a command from history



If you want to run a command that was executed in the past, you can search the command by running the command "history".

To run any of the listed commands there is not need to type the commands you can use the command number listed next to it.



Tip 8: Number of history commands



The number of commands that history can store is indicated by the variable HISTSIZE. To view the value print it using



The default value is generally 1000, but it can be by changing the value in the file .bashrc.

Tip 9: Listing files by their file type or extension



The command "ls" is used to list the files on the linux terminal. By default the command will list all the file of the specified directory. But if you want to view only files by their type, for example only pdf files then we can use their extension to filter the output of the command

The above command should list all the files that have the extension of pdf in their file name. Similarly you can use any extension to filter the output.


Tip 10: Create hidden files in linux



To make any file to act a hidden file, that is it does not get listed by default in GUI or in the terminal, you can prefix the file with a "."



In the above example, files hello and hello12 were listed as output of ls command. Then we created a empty text file by the name ".hidden_hello". But the new file did not get listed in the output of ls because of the dot that was prefixed . To be able to view the hidden files we have the pass the option "-a" to ls.



Tip 11: Add line numbers to every line of a text file



Let us say we have a text file, and we want to add line numbers to every lune. We can use cat command to do that.

If we have a text file called hello.

we can use the option "-n" with cat to prefix with line numbers



The output can be redirected to a new file to create a file with every line numbered.



We can add the "-n" option and every line will be added with a line number, which can be redirected to a new file.

Tip 12: Finding the shell being run



The following command can be used to find the current shell being run.


Tip 13: Command to find the architecture of CPU



To find the architecture of the CPU of the system, we can use the following command



The above output indicates that the architecture is x86_64 meaning it is a 64 bit architecture.

Tip 14: Searching for a string in a file



Assume we have a file names "hello" with the following contents

if we want to search for the string "linux" in the file we can use the command "grep" as follows

Tip 15: rm: cannot remove ....: Is a directory



The above error comes if the command rm is being used to delete a directory. An extra option "-r" has to be passed to the command to get around this error , because by default rm only deletes files.

The "-r" option will go into the directory, delete all the files and then delete the directory too. Therefore use



Tip 16: rmdir: failed to remove ....: Directory not empty



The rmdir command is meant only to remove directories which do not have any files in them. If the directory is not empty it will throw the error shown above.

One way to go around this is to go into the directory and delete all the files , the easier way is to use the option mentioned in the Tip number 15 above.

Tip 17: Shutdown a system automatically after some delay.



If the should automatically shutdown at a specified time or after a specific delay we can use the command shutdown.


To shutdown at a specific time



The above command will shutdown the system at 11:30 PM automtically.

To shutdown after a specific delay



The above command will shutdown the system 10 minutes after the command has been issued.

Tip 18: View the last lines of a file as file is written to



Very often we might want to keep a watch on the logs of programs, but logs can run into hundreds of lines making it diffcult to keep track of it. The command tail helps us view the last few lines of a file, and the output automatically updates as the file is written to.

The command can be used as shown below



The command will output the last 10 lines of the file and wait for the file to be updated, and keep showing the output as the file is written to.Use control+c to exit .

Tip 19: Listing file names with special charachters escaped



If we have a space or some of the other special charachters as part of the name of a file, we need to add an escape sequence of "\" behind it to make sure it is treated as a part of the file name. While listing the file names using "ls" we can ensure that the escpae charachter is added by default whenever needed by using the option -b.

We can see in the example above that the name of the file "Hello World" has a space in between the two words. with out the -b option the output of ls puts the name in ' ' to ensure it is treated as one file, but with the option -b the space is prefixed with an escape sequence and the ' ' are removed.

Tip 20: Listing file names in comma seperated format

If output of the list in a csv file, the listing can be forced to appear in a csv format using the option -m



Tip 21: Listing file names sorted based on extenstion

To group file names by their extenstion, like group all .pdf file together, all .odp file together etc, we can pass the extenstion -x to the ls command



Tip 22: Logging the output of a command to a file

The command logsave can be used to log the output generated from a command to a file automatically with out using redirection.

Syntax





The above command will log the output of ls to the file named logfile,and also display the same on the terminal. We can use the option -a in case the output has to be appeneded to an existing file.

Tip 23: Finding the size of RAM in the system

We can use the following command to find the total amount of RAM space in the system and how much is being used.

Syntax



The first line of the output indicates the RAM size and the second line gives the size of the swap space.

Tip 24: Counting the number of words or charachters in a pdf file.

Using pdfgrep and the wc command we can get an approximate of number of words or chrachters that are present in pdf file with out opening the file.



Instead of -w we can pass the option -c to get the charachter count. If pdfgrep command is not found ,it can be installed from the package manager.

Tip 25: Access the hardware clock from command line

The time being maintaied by the hardware clock can be displayed using the command hwclock,the same can also be used to set the systemclock and also update the hardware clock if needed. But to be able to access the hardware clock we need super user permissions and hence the command needs to be used with sudo.



We can see the output as the current time being manitained at the hardware, by using the option -s we can set the same time as the system time .

Tip 26: Command to search for string at the beginning of a line.

The command look can be used to search for lines in a file that begin with a specific string. For example, if we have a file hello,txt



If we want to search for the string This in the above file. search for lines that begin with the string This.

The command is case sensitive by default.

Tip 27: View contents of a MS word Document from command line.



The command wordview can be used to see the contents of a doc file.

The command will open the wordfile text.doc for viewing. Note that this allows only to view and not to edit the files.

Tip 28:Here is a Script to know the number of system calls in the linux kernel.





In linux 5.11 the number comes to be 346.

Tip 29:Here is a Script print out the logical names of the network interfaces .





The output will be the names of the network interfaces availale in the system. In case you get the error that lshw is not found, it can be installed from the respective package manager of the distribution.

Tip 30:bash: cd: too many arguments



The change directory commannd can throw the error of "too many arguments" if the name of the directory has a space in it . For example, if we have a directory names "test one" and we run the command



The issue can be handled without renaming the file by escaping the space charachter or putting the name of the file in double quotes.



Both the above way of passing the directory name will prevent "cd" from throwing the error of two many arguments.

Tip 31:Small script to find if an item is a file or a directory on command line



Here is small set of commands which will tell us if a given item is a directory or a file. Generally on the terminal it can be identified using the color or by looking at the fist letter of the ls -l command. Here we try to achieve the same using the stat command.



In the above set of commands, replace "Desktop" with what ever name of the item that you want to verify.



Tip 32:Python error : name 'operator' is not defined



The error is thrown when we use the operator module for accessing one of related functions for example operator.itemgetter etc. So we need to import the operator module to get around the error.



No comments:

Post a Comment