Sunday, November 9, 2014

UNIX basics for software testers

Overview

As a software tester, you need to be familiar with different operating systems, this guide will provide the basic list of commands that you need to know when working with UNIX based systems. 

The ‘ls’ command:

This command will help you to reveal the list of objects (files/folders) under a given folder.

Command
output
ls
This command will list all files/folders with names only

ls –l
This command will list all files/folders, with additional fields (name of object, permissions, size, owner and more).

ls –a
Display all objects, including the invisible files for “-a” switch

ls -F
Add special characteristics for file system objects(Folder/File) that identify is type ,Examples:
“/” – The object is a directory
“@” - The object is a symbolic link

ls *StringToSearch*
Will return all files that contain the requested string
Example :
Folder has two files : DavidTzemach.txt , DtVisionTech.ps1
Search query: ls *Tz*
Result : the returned value from this query will be a single file (DavidTzemach.txt)

ls Char*
List all files that Starts with the specified char

Example :
Folder has four files :
aFile.a |bFile.b | cFile.c |dFile.d

Search query: ls *a
Result : the returned value from this query will be a single file(aFile.a)

ls *Char
List all files that Ends with the specified char
Example :
Folder has four files :
aFile.a |bFile.b | cFile.c |dFile.d

Search query: ls c*
Result : the returned value from this query will be a single file (aFile.c)

ls *.String
List all files that Ends with the specified extension
Example :
Folder has four files :
File.txt |File.bat | File.doc |File.xls

Search query: ls *.bat
Result : the returned value from this query will be a single file(File.bat)

 

The ‘cd’ and ‘pwd’ commands:

This command will help you to navigate between different folder hierarchies (Root/Subdirectories…)
cd = Change Directory

Command
output
pwd
This command will print the current working directory (Staring from the mount export).

cd “Destination”
That’s the basic way to navigate between folders, the “Destination” folder can indicate for :
-          Sub Directory
-          New path
-          ETC.

Example :
pwd  =  /
cd test
pwd  =  /test
cd  /test//test1 /test2/test3//test4//test5
pwd = /test//test1 /test2/test3//test4//test5
Current working  location = test5

cd
cd ..
cd
Return to home folder

cd..
Return one level up
Example :
Current : /Root/Test/
Run : cd
Current : /Root

cd /
Return to root folder (“/”) that now becomes the working directory

Folders and Files – basic commands

This table contains a list of commands that can be used when you need to manipulate system objects

Creating new folders

Command
output
mkdir –help
Will open the command help menu

mkdir “NewFolder”
This command will create a subdirectory in your current working directory

Example :
Mkdir NewFolder = new folder is created with the specified name

Remove system objects


Command
output
rmdir “ObjectToRemove”
Remove EMPTY folders(in any case of existing objects(Folders, Files..), the operation results in a notification error)

rm





Remove Files/folders that contain objects (Folders, Files...)

Examples :

rm “File Name” – remove a specific file

rm – r “Object Name” = delete the object with all his content (Subdirectories/Files), each object removal should be approved.

rm Startswith* - delete all files that start with specific char/s

rm *EndsWith  - delete all files that ends with specific char/s


Rename System objects


Command
output
mv  FolderName  NewFolderName
Rename folder name

Example: mv  A B
Result : Folder A will rename to folder B
mv  FileName  NewFileName
Rename File name

Example: mv  F1 F2
Result : File F1 will rename to folder F2

Copy system objects


Command
output
cp  originalFile  new file
This command will copy an existing file(current directory) and clone it with a new file name

Example:
Directory Name: Test 1
Current files: Single file (original)
cp original tmpnew

Result:
Folder Test 1 will now have two files(original , tmpnew)
cp  originalFile  New destination
This command will copy an existing file(current directory) and clone it to a new folder, the file is created with the same name

Example:
Directory Name: Test 1
Current files: Single file (original)
cp original /destination/NewFileNmae

Result:
The “destination” Folder will now have the clone file(original)
cp  originalFile  New destination/NewName
This command will copy an existing file(current directory) and clone it to a new folder, the file is created with the new name that specified by the user

Example:
Directory Name: Test 1
Current files: Single file (original)
cp original /destination

Result:
The “destination” Folder will now have the clone file(original)
cp  *.End_Filename /destination
Will copy all files that end with the specified char/string
cp  Filename.* /destination
Will copy all files that Starts with the specified char/string
cp  folder name /cloned foldername
Will clone a directory to the current folder, the folder name is specified by the user
cp –r  A /B
Will copy folder A (recursively) and named it “B”, the new folder will created under the current folder location
cp –r  A /B –v
Same as previous command, but now the -v switch will add an additional information about the execution step
cp –r  A /B –u
Will copy files to a new destination folder, only if the files are missing OR with the older date from the source files

Moving System objects


Command
output
mv  FileName NewPath
Rename the file name on current directory name

Example: mv  A /TmpFolder
Result : File A will be moved to TmpFolder
mv  FileName NewPath/NewName
Rename the file name on current directory name

Example: mv  A /TmpFolder/AAA
Result : File A will be moved to TmpFolder with new name(AAA)

 

Managing files content


Command
output
cat File.txt
Will display the file content on screen
cat File.txt -n
Will display the file content on screen + line numbers
cat File.txt -b
Will display the file content on screen  + Unique id number per line (Empty lines are excluded)
cat /FileA > /folder/tmpfile.txt
Instead of printing the file content to the screen. All the data will be written totmpfile.txt
cat FileA >FileB
Will create new file(FileB) with the content of FileA
cat FileA /FileB
Will aggregated the data of fileA and FileB (Content will be available on the command line session).
cat >>FileA
In case you want to modify a specific file content
cat FileA FileB  >Content.txt
Will aggregated the data of fileA and FileB (Content will send to “Content” file).
cat BigFile | less
In case that you want to display files with very large content, you can use the “|”, this switch will help you to control the output in the screen.

The content can be controlled with few basic chars:

Space – display the next available lines
B – display the previous syntax
q -  quit

diff  fileA fileB
Display the content differences between two files

File Creation


Command
output
cat  file1
New file is created (file1), the user can add content.

Navigation keys:
Enter – new line
Ctrl+d – Save and Exit
Ctrl+c – Exit

 

How to Search content in file(less/grep)

Command
output
Option 1: less
Step 1 : less file.doc
Step 2 : /SearcCriteria

Option 2: grep
grep SearchCriteria file.txt








Option 1:
In step 1, we open the file and print the content to the screen
In step 2, we can enter the “Search” criteria

Option 2:
All lines that have the search criteria will be printed on screen

Additional and basic options:
-c  =  Total count of matched lines
-v  =  Display lines that doesn’t match the search criteria
-H  =  Matched lines, with the file name at the start

Help Commands


Command
output
whatis command_name
Short summary of the command
apropos keyword
Will list all available commands with this keyword
man command_name
Returns the manual page of the specified command

 

System processes

Command
output
ps - A
Select all processes with the unique process identifier(PID)
ps - e
Select all processes with the unique process identifier(PID)
ps -aux
Display all processes (All Users)
ps –p [process ID]
Select a single process based on the searched ID
ps – A --forest
List all process in the hierarchy
top
Display the system statistics and number of executions

 

General Commands


Command
output
clear
This command will remove all text from the command screen
exit
Close connection session

Environmental Commands


Command
output
uptime
All the date and time information since the machine is in running state
hostname
The server hostname
date
Display the machine currently date
cal
Display the machine calendar
df
Display the space left per volume
arch
Display the server architecture
who
who –b
Display all the current connected users
Display the system last reboot time


No comments:

Post a Comment

My Presentations