http://www.tecmint.com/20-advanced-commands-for-middle-level-linux-users/
20 Advanced Commands for Middle Level Linux Users
You might have found the first article very much useful, this article is an extension of the 20 Useful Commands for Linux Newbies. The first article was intended for newbies and this article is for Middle-Level-User and Advanced Users. Here you will find how to customise search, know the processes running guide to kill them, how to make your Linux terminal productive is an important aspect and how to compile c, c++, java programs in nix.
21. Command: Find
Search for files in the given directory, hierarchically starting at the parent directory and moving to sub-directories.
Note: The `-name‘ option makes the search case sensitive. You can use the `-iname‘ option to find something regardless of case. (* is a wildcard and searches all the file having extension ‘.sh‘ you can use filename or a part of file name to customise the output).
Note: The above command searches for all the file having extension ‘tar.gz‘ in root directory and all the sub-directories including mounted devices.
Read more examples of Linux ‘find‘ command at 35 Find Command Examples in Linux
22. Command: grep
The ‘grep‘ command searches the given file for lines containing a match to the given strings or words. Search ‘/etc/passwd‘ for ‘tecmint‘ user.
Ignore word case and all other combination with ‘-i‘ option.
Search recursively (-r) i.e. read all files under each directory for a string “127.0.0.1“.
Note: You can use these following options along with grep.
- -w for word (egrep -w ‘word1|word2‘ /path/to/file).
- -c for count (i.e., total number of times the pattern matched) (grep -c ‘word‘ /path/to/file).
- –color for coloured output (grep –color server /etc/passwd).
23. Command: man
The ‘man‘ is the system’s manual pager. Man provides online documentation for all the possible options with a command and its usages. Almost all the command comes with their corresponding manual pages. For example,
Manual page for man page itself, similarly ‘man cat‘ (Manual page for cat command) and ‘man ls‘ (Manual page for command ls).
Note: man page is intended for command reference and learning.
24. Command: ps
ps (Process) gives the status of running processes with a unique Id called PID.
To list status of all the processes along with process id and PID, use option ‘-A‘.
Note: This command is very useful when you want to know which processes are running or may need PIDsometimes, for process to be killed. You can use it with ‘grep‘ command to find customised output. For example,
Here ‘ps‘ is pipelined with ‘grep‘ command to find customised and relevant output of our need.
25. Command: kill
OK, you might have understood what this command is for, from the name of the command. This command is used to kill process which is not relevant now or is not responding. It is very useful command, rather a very very useful command. You might be familiar with frequent windows restarting because of the fact that most of the time a running process can’t be killed, and if killed it needs windows to get restart so that changes could be taken into effect but in the world of Linux, there is no such things. Here you can kill a process and start it without restarting the whole system.
You need a process’s pid (ps) to kill it.
Let suppose you want to kill program ‘apache2‘ that might not be responding. Run ‘ps -A‘ along with grepcommand.
Find process ‘apache2‘, note its pid and kill it. For example, in my case ‘apache2‘ pid is ‘1285‘.
Note: Every time you re-run a process or start a system, a new pid is generated for each process and you can know about the current running processes and its pid using command ‘ps‘.
Another way to kill the same process is.
Note: Kill requires job id / process id for sending signals, where as in pkill, you have an option of using pattern, specifying process owner, etc.
26. Command: whereis
The ‘whereis‘ command is used to locate the Binary, Sources and Manual Pages of the command. For example, to locate the Binary, Sources and Manual Pages of the command ‘ls‘ and ‘kill‘.
Note: This is useful to know where the binaries are installed for manual editing sometimes.
27. Command: service
The ‘service‘ command controls the Starting, Stopping or Restarting of a ‘service‘. This command make it possible to start, restart or stop a service without restarting the system, for the changes to be taken into effect.
Startting an apache2 server on Ubuntu
Restarting a apache2 server on Ubuntu
Stopping a apache2 server on Ubuntu
Note: All the process script lies in ‘/etc/init.d‘, and the path might needs to be included on certain system, i.e., in spite of running “service apache2 start” you would be asked to run “/etc/init.d/apache2 start”.
28. Command: alias
alias is a built in shell command that lets you assign name for a long command or frequently used command.
I uses ‘ls -l‘ command frequently, which includes 5 characters including space. Hence I created an alias for this to ‘l‘.
check if it works or not.
To remove alias ‘l‘, use the following ‘unalias‘ command.
check, if ‘l‘ still is alias or not.
Making a little fun out of this command. Make alias of certain important command to some other important command.
Now when your friend types ‘cd‘, just think how funny it would be when he gets directory listing and not directory changing. And when he tries to be ‘su‘ the all he gets is the location of working directory. You can remove the alias later using command ‘unalias‘ as explained above.
29. Command: df
Report disk usages of file system. Useful for user as well as System Administrator to keep track of their disk usages. ‘df‘ works by examining directory entries, which generally are updated only when a file is closed.
For more examples of ‘df‘ command, read the article 12 df Command Examples in Linux.
30. Command: du
Estimate file space usage. Output the summary of disk usages by ever file hierarchically, i.e., in recursive manner.
Note: ‘df‘ only reports usage statistics on file systems, while ‘du‘, on the other hand, measures directory contents. For more ‘du‘ command examples and usage, read 10 du (Disk Usage) Commands.
31. Command: rm
The command ‘rm‘ stands for remove. rm is used to remove files (s) and directories.
Removing a directory
The directory can’t be removed simply by ‘rm‘ command, you have to use ‘-rf‘ switch along with ‘rm‘.
Warning: “rm -rf” command is a destructive command if accidently you make it to the wrong directory. Once you ‘rm -rf‘ a directory all the files and the directory itself is lost forever, all of a sudden. Use it with caution.
32. Command: echo
echo as the name suggest echoes a text on the standard output. It has nothing to do with shell, nor does shell reads the output of echo command. However in an interactive script, echo passes the message to the user through terminal. It is one of the command that is commonly used in scripting, interactive scripting.
creating a small interactive script
1. create a file, named ‘interactive_shell.sh‘ on desktop. (Remember ‘.sh‘ extension is must).
2. copy and paste the below script, exactly same, as below.
Next, set execute permission and run the script.
Note: ‘#!/bin/bash‘ tells the shell that it is an script an it is always a good idea to include it at the top of script. ‘read‘ reads the given input.
33. Command: passwd
This is an important command that is useful for changing own password in terminal. Obviously you need to know your current passowrd for Security reason.
34. Command: lpr
This command print files named on command line, to named printer.
Note: The ‘lpq‘ command lets you view the status of a printer (whether it’s up or not), and the jobs (files) waiting to be printed.
35. Command: cmp
compare two files of any type and writes the results to the standard output. By default, ‘cmp‘ Returns 0 if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.
To provide examples for this command, lets consider two files:
file1.txt
file2.txt
Now, let’s compare two files and see output of the command.
36. Command: wget
Wget is a free utility for non-interactive (i.e., can work in background) download of files from the Web. It supports HTTP, HTTPS, FTP protocols and HTTP proxies.
Download ffmpeg using wget
37. Command: mount
Mount is an important command which is used to mount a filesystem that don’t mount itself. You need root permission to mount a device.
First run ‘lsblk‘ after plugging-in your filesystem and identify your device and note down you device assigned name.
From this screen it was clear that I plugged in a 4 GB pendrive thus ‘sdb1‘ is my filesystem to be mounted. Become a root to perform this operation and change to /dev directory where all the file system is mounted.
Create a directory named anything but should be relevent for reference.
Now mount filesystem ‘sdb1‘ to directory ‘usb‘.
Now you can navigate to /dev/usb from terminal or X-windows system and acess file from the mounted directory.
38. Command: gcc
gcc is the in-built compiler for ‘c‘ language in Linux Environment. A simple c program, save it on ur desktop as Hello.c (remember ‘.c‘ extension is must).
Compile it
Run it
Note: On compiling a c program the output is automatically generated to a new file “a.out” and everytime you compile a c program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.
Compile it this way
Here ‘-o‘ sends the output to ‘Hello‘ file and not ‘a.out‘. Run it again.
39. Command: g++
g++ is the in-built compiler for ‘C++‘ , the first object oriented programming language. A simple c++ program, save it on ur desktop as Add.cpp (remember ‘.cpp‘ extension is must).
Compile it
Run it
Note: On compiling a c++ program the output is automatically generated to a new file “a.out” and everytime you compile a c++ program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.
Compile it this way
Run it
40. Command: java
Java is one of the world’s highly used programming language and is considered fast, secure, and reliable. Most of the the web based service of today runs on java.
Create a simple java program by pasting the below test to a file, named tecmint.java (remember ‘.java‘ extension is must).
compile it using javac
Run it
Note: Almost every distribution comes packed with gcc compiler, major number of distros have inbuilt g++ and java compiler, while some may not have. You can apt or yum the required package.
Don’t forget to mention your valueable comment and the type of article you want to see here. I will soon be back with an interesting topic about the lesser known facts about Linux.