As a software tester, knowledge of basic Linux commands is a necessary. Before going in to details commands we should be aware of some of the facts of Linux OS.
- You can do almost anything in a terminal which you can also do from GUI
- Most command was first designed to work in the terminal then GUI was put on top. Hence some of the GUI is not well designed and may be buggy.
- Your current directory or folder(for windows users terminology) can be noted 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 same as /Home or /HOME
- Use the tab key to complete file names. If you have a long driver titled, for example,
- driver-128947232jaseu.sh, 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.
- Almost any command can be read about in full using the manpage or by typing -h or –help after writing the initial command. This syntax is either man command_name, command_name -h, orcommand_name –help.
- Almost any command can also explicitly display what is happening. This is done usually by the -v or–verbose
BASIC LINUX COMMANDS
- cd This will move you back to your home, same as cd ~
- cd .. This will take you back exactly one directory. Starting in /home/xxx/Desktop, cd .. will put me into /home/xxx. This can be expanded upon, cd ../../ from the Desktop location instead will move me 2 back, from my Desktop to /home.
- cd foldername/ This will move you forward to the given folder in your current folder. Take note of the missing prefix / it is an important omission. if I am in /home/xxx and I want to get to Desktop, I must type cd Desktop/ without the / before Desktop. Typing / before it places us in the root of file system, which is incorrect.
- cd /some/other/path This will take you to the specified folder path, supposing it exists as typed exactly. Don’t forget your tab completion!
- ls By itself, ls will simply list all your files in the current folder.
- ls -l Provides a longer listing format including owners, permissions, size, and date modified.
- ls -a Displays hidden files and folders as well as the normal listing.
- ls -al Combine options to display both hidden files and in the long format.
- ls -h Show file sizes in human readable format (K, M, Gbyte) filesizes instead of bytes. Often used in conjuction with the -l flag.
- You can view files in directories you are not even in. If I am in /home/justin/Desktop, and I want to view a file in /home/justin, I can do ls ../ list files one directory back (and not have to go back to do so.)
3. cp –> copy files
- cp file /path/to/folder Copies specified file to the given path.
- cp -r folder /path/to/folder Copies recursively the contents of the folder to another folder.
- cp *.extension /path/to/folder Copies 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 exis.
- cp name* /path/to/folder Copies all 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 exist.
4. mv –> move files
- rm file Remove the specified file from the system.
- rm -r folder Remove the specified folder from the system
- rm -rf folder Removes the specified folder forcefully from the system. This command can severely break your configuration if used incorrectly as it will not prompt you if something critical is being deleted. If you have to use this, chances are something more is broken or there was a mistake made. This should only be used as an absolute last resort method and is not recommended.
6. mkdir –> Make directories
- 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/xxx/newfolder/2ndfolder, and only /home/xxx exists, using mkdir -p will make both directories newfolder and 2ndfolder.
7. ps –> List Processes
8. kill / killall / xkill –> Kill offending processes.
- kill PID PID is a process id number number of the process which need to be killed. One should obtain the PID from a command like ps. If a process refuses to die, one can alternatively specify kill -9 PID which should terminate the process forcefully.
- killall program Killall kills *by name* all instances of said program. If there are for example 3 firefox sessions open, killall firefox will do just that; kill all firefox sessions.
- xkill is a GUI way to click and kill windows. Typing in xkill should present a skull-and-crossbones icon, and the next window clicked on will be killed.
Some TIPS in using Linux
- Appending new path to PATH variable.
- Lost yourself in a directory?
Type pwd to print working directory
- Want to calculate your disk space quickly?
df -h can give you a quick checkup
- Want to calculate the size of a folder or file quickly?
du -cksh target_name can do exactly that.
- Want to calculate the size of the current folder?
- Need to mark a file executable?
chmod +x filename can do that. To give all permission use chmod 777 filename.