Linux Essentials for Testers

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.

Facts

  • 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

1. cd –> Used to navigate the directories.  You can move to any location by path.
  • 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!
2. ls –> Used to list folder contents.  You can view many kinds of file and folder attributes.
  • 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

5. rm –> Remove 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.
Caution:
Removing files via rm is permanent.  It does not use the Trash bin.  Use with caution and make sure you are deleting explicitly what you want, not what you think you want.

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.
9. Pipes (|) –> Redirecting output of a program to another program.
Pipes takes output from one program and route it to be used as input of another program. example- who | wc -l or ps aux | wc -l
10.  > and >> redirectors –> Se nd output to a file instead of terminal
        > is used to overwrite currently existing file and >> is used to append information to the file
Example: ls -l > file1 or ls -l >> file2
11. sudo execute commands with root rights

Some TIPS in using Linux

  • Appending new path to PATH variable.

             export  PATH=$PATH:/home/xxx/yyy/tools

  • 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?

              du -cksh

  • Need to mark a file executable?

             chmod +x filename can do that.  To give all permission use chmod 777 filename.

Leave a Reply