If you are looking to use the Terminal Shell in Ubuntu Linux and how to find out more about how to use the many commands available in Terminal then you will find the following article of use.
In this article I'll explain how to use the inbuilt Terminal commands to provide information on what a particular command is and what it does.
I'll provide a couple of examples to illustrate the process as well.
Simply put, the command line is powerful. An army of tools exist that can take what would be a tedious job in a graphical User interface (GUI) program and turn it into a task that takes just a few seconds. Removing the last four lines in every row of a large file would be a lengthy process in a GUI application, but can become easy and automated on the command line.
There are a large number of commands associated with the Terminal Shell in Ubuntu Linux. The article below lists the majority under the groups they affect.
There are three main commands we will be using to provide all the information needed to use the various terminal commands.
The man command displays the manual for any command used in the Terminal.
The Majority of shell commands will accept various options. To get some information about a command and a list of the available options use the man - short for manual command. Give the name of the command you want to find more about as its argument. i.e. which command the man command will work on.
Example: After entering the command man who, the following output is displayed.
Many of the manuals are several screens long. You will need to use the cursor/ARROW keys or the Page Up/Page Down keys to navigate through the text. The space bar key shows the next line of text. Pressing the q key quits the manual and returns to the shell.
We can see from the man page that we can use the -a option to display more information about the currently active users sessions.
As well as reading help files for programs, the man command can also give information on shells, functions and libraries.
If you’re not sure how to use a specific command, run the command with the -h or –help switches. You’ll see usage information and a list of options you can use with the command.
Example: If you want to know how to use the wget command, type wget –help or wget -h.
This often prints a lot of information to the terminal. It can be unwieldy to scroll through. To read the output more easily, you can pipe it through the less command, which allows you to scroll through it with the cursor/ARROW keys on your keyboard.
Example: wget -help | less
Press q to close the less utility when you’re done.
To find a specific option, you can pipe the output through the grep command. For example, use the following command to search for options that contain the word proxy.
wget -help | grep proxy
Some commands are also documented inside the info system. The information here complements what is offered by the man command.
Example: After entering the command info ls the following output is displayed.
You can now view a manual with important information on the ls command.
You can scroll down the pages by pressing the space bar key or by using the cursor/ARROW keys.
For help, press H.
To quit, press Q.
To search for a menu item, press M.
You can do pretty much anything in a terminal, which you would also do from a GUI.
As said before many commands were designed first to work in the terminal and then a GUI was put on top. That's why some GUI's may feel clunky - at first they were an afterthought as most Linux Server builds don't bother with them.
The default location for your terminal to open from the menu is in your home folder. Known as ~
You can find your current directory by the . operator. Most commands when they act on the current folder selection operate on .
Commands, locations and files are case sensitive. /home is not the same as /HOME or /Home.
Use the TAB key to complete file names. If you have a long driver title. driver-128947232jaseu.sh for example, simply type dri and it will fill in the rest. provided you don't have 2 names starting with dri and if you do, add another character to make it driv and try again. Just make sure the auto complete matches what you want to see.
Almost any command can be read about in full using the man page or by typing -h or --help after writing the initial command. This syntax is either man command_name, command_name -h or command_name --help.
To get even more information, you can use info. A command can be searched for by using info command_name. For most of these commands which are part of the coreutils package, one can find info as well using info coreutils command_name invocation where command_name is replaced by the command searched for.
Almost any command can show exactly what is happening, step by step. This is done usually by the -v or --verbose.
You can specify multiple command flags to a command at a time to get more information. See the ls -al example below.
Command names are not always obvious - due to space limitations in the old days of Unix they were shortened and these conventions stuck.
cd - Moves you back to your home, same as cd ~
cd.. - Takes you back one directory. Starting in /home/user/Desktop, cd.. will put you into /home/user. This can be expanded to cd ../../ which will move you back 2 directories to /home.
cd foldername/ - Move you forward to the given folder in your current folder.
cd /some/other/path - Takes you to the specified folder path. As long as you got the path right. Don't forget you can use the TAB key to autocomplete.
ls - Will simply list all your files in the current folder.
ls -l - Provides a longer list including owners, permissions, size, and date modified.
ls -a - Displays the hidden files and folders as well as the normal list.
ls -al - Combines two options to display both the hidden files and folders and do it in the long format.
ls -h - Shows file sizes in human readable format KB, MB, GB, filesizes instead of bytes. Most often used in combination with the -l flag.
You can view files in directories you are not even in. If I am in /home/user/Desktop and I want to view a file in /home/user, I can do ls ../ which will list the files one directory back.
cp file /path/to/folder - Copies the specified file to the given path.
cp -r folder /path/to/folder - Copies repeatedly the contents of the folder to another folder.
cp *.extension /path/to/folder - Copies the files matching the given extension to the new folder. To copy all .doc files, it becomes cp *.doc /path/to/folder and the folder must exist.
cp name* /path/to/folder - Copies all of the files starting with name to the given folder. To copy all files starting with example, it becomes cp example* /path/to/folder and the folder must already exist.
The syntax of mv is similar to the example above with cp exempt for example #2. mv does not take the -r flag since moving a folder also moves its contents. The syntax is not exact in all instances, but works with the above examples. Consult your manpages for more details.
Removing files via rm is permanent. It doesn't use the Trash bin. Use this with caution and make sure you are deleting exactly what you want, before you hit the Enter key. If you overcomplicate your delete commands, it probably won't end well.
rm file - Remove that specific file from the system.
rm -r folder - Remove that specific folder from the system.
rm -rf folder - Removes that specific folder forcefully from the system. This command can mess up your configuration if it's used incorrectly.
One can edit files using nano in a terminal to do quick and rough files all the way up to full configurations. It can be useful, but it handles plain text files and programming files, so things like MS Word documents will not open properly.
If a file is owned by root, a regular user can't edit it. nano must be prefixed with sudo; in order to save changes. It will open in read-only mode otherwise.
nano newfile.whatever of the specified name and opens it for editing.
nano existing_file - opens the existing file for editing.
From inside nano
Save the file by pressing the CTRL+O keys together and either change the name or press the Enter key to keep the same name. This will save the file.
Exit nano by using the CTRL+X keys together. If you have unsaved changes, then it will ask if you want to save them.
mkdir folder_name - Creates the folder with the specified name
mkdir -p /path/to/folder/name - Creates each folder as necessary. To create folder /home/user/1stfolder/2ndfolder, and only /home/user exists, using mkdir -p will make both directories 1stfolder and 2ndfolder.
ps aux - Lists all the processes in detail running on the system. This includes user, Process ID PID and name of process. Using this you can view the process list and if necessary you can kill unnecessary or stalled processes.
kill PID - PID is a number referencing the offending process. You should obtain the PID from a command like ps aux. If a process refuses to die, you can also specify kill -9 PID which should terminate the process by any means.
killall program - Killall kills by name all instances of the listed program. If there are for example 3 Firefox internet browser sessions open, killall Firefox will do just that, kill all Firefox sessions. kill would simply take the specified PID of the offending Firefox process you wish to kill and kill that one only.
xkill is a GUI way to click and kill windows. Typing in xkill should bring up a skull-and-crossbones icon and the next window clicked on will be killed.
Pipes are represented by the straight bar otherwise known as the | key.
Its a rarely used key in Windows, but it is often found on the backslash key.
These are used to link commands together. Pipes take the output of one command and route it to be used as input for a second command when they are chained together.
Consult online resources for further information about pipes and their use as there are volumes written on them.
> is used to overwrite currently existing files contents by replacing them with the output from the new command.
>> is used to append information to currently existing files. This is useful for logging actions.
Example: ps aux > processes.log, sends the output of ps aux to the file processes.log for viewing the command output in a text editor and overwrites the current contents of the file.
tee is used in conjunction with a | in order to take the command output and send it somewhere else. This is useful if there are errors which which you miss. This way whatever goes on the screen is also captured to a file.
Example: dmesg | tee boot.txt would run the command dmesg which shows the initial boot info, and the | sends the output of dmesg to tee, which then does its job by sending it to the terminal and to the log file boot.txt.
If you need to execute a file in the current directory after it is marked executable, then the ./ operator can execute the file as a normal user provided you do not need root rights. ./ literally means in the current directory so it does not work on files outside of the present directory.
If you need to execute a file not in the current directory, then you must pass the path to the proper executing program. If it is a python program, it will be python /path/to/file and if its a shell file, its sh /path/to/file as an example. There are of course other programs, but these will be the most common.
If you need to execute a file with root rights because you received operation not permitted?. You need to prefix the command with sudo. As with the above example, sudo python /path/to/file will execute the script with root rights.
If you need to execute a GUI program from the terminal, then simply type the program name - case sensitive! and it will launch. This will render the current terminal unusable. Closing the terminal while the program is open will also kill the program. A better way is to background the program, using program_name & and then typing the word exit at the terminal to close it and keep the process running.
If you need to run a GUI program with root rights from the terminal, then prefix it with gksudo or gksu and not sudo. Using sudo to launch GUI applications is a bad habit and should be avoided.
Do not use sudo just because something bring up Operation not permitted. Keep in mind that you can destroy systems by running commands in the wrong place with root rights. Make sure your files come from reputable sources.
Lost yourself in a directory? Type pwd to print working directory.
Want to calculate your disk space? df -h can give you a quick figure.
Want to calculate the size of a folder or file? du -cksh target_name will do exactly that. Want to calculate the size of the current folder? du -cksh.
Want to mark a file executable? chmod +x filename will do that.
Need to mount an iso? Linux has this functionality built in. Simply create a directory somewhere, say /home/user/isomount and issue the command mount -o loop /path/to/myisofile.iso /home/user/isomount and the contents will be mounted inside that folder.
Having run a command, you need to re-run it, but you can't remember exactly how it went? Type history into the terminal and it will print out your command history. Want to clear your history? history -c will wipe the information.
Article ID: SLN265948
Last Date Modified: 10/18/2018 03:35 AM
Thank you for your feedback.