These LINUX commands should be common to most implementations and variants of the operating system. Your LINUX version may not support all the commands, and will almost certainly have extra commands not mentioned here. If you use UNIX then most of these commands will be familiar.
If you are entering a long command that needs to span more than one line, then finish each line off with a backslash as this indicates the command will continue on the next line, without an Enter separating one line from the other.
- cat
-
Used to view the content of a file, the content is usually sent to your monitor
Syntax:
>cat filename1 filename2 ...
- cp
-
used to copy data
Syntax:
>cp [-R] sourcefile targetfile
The source and target files can be directories, then the –R option will copy all files and subdirectories under the source directory.
- file
-
Used to determine the format of a file
Syntax:
>file [-z] filename
The results should not be taken as absolute, but more of an educated guess. The –Z option lets ‘file’ investigate compressed files.
- grep
-
looks inside a file, and prints out all lines that contain a search pattern.
Syntax:
>grep PATTERN file
Grep is often combined with other commands to process output.
Special characters
‘.’ Exactly one character
‘*’ 0 or more characters
‘?’ 0 or 1 instance of the character preceding the ? for example cm? matches ‘c’ or ‘cm’
[………] range for example [1.6] = 123456
‘^’ the start of a line
‘$’ the end of a line
‘/’ is the escape character and is used to nullify special characters when placed before them, for example ‘why/?’ will be parsed as ‘why?’ It can also give a special meaning to non-special characters, like ‘(’ and ‘)’ for example grep ‘search\(ing)\?’ will look for ‘search’ and ‘searching’
To search for the ‘example’ abbreviation e.g. use grep e\.g\. filename
- head
-
Look at the beginning of a file
Syntax:
>head [-n number] filename
the –n option specifies how many lines to display
- less
-
The GNU file viewer version of the ‘more’ command
Syntax:
>less filename
- ln
-
Define a link or a shortcut from your directory to a file in another directory
Syntax:
>ln [–s] target_file link_name
The –s parameter means that this is a symbolic link, and they can span partitions. Without the –s the link is a hard link and they cannot span partitions. A soft link is just a pointer, which a hard link is the same size as the original file. is particularly interesting for beginning users: they are fairly obvious to see and you don't need to worry about partitions.
- locate
-
Print all accessible files matching the search pattern. This command requires that the slocate database has been set up. If it has not, you can do this by entering the slocate –u command. Locate will only return files that you have access rights to.
Syntax:
>locate searchstring
- ls
-
Lists the contents of a directory
Syntax:
>ls –l
is probably most the often used but there are lots of other parameters which can be combined as ls –liap for example. Some parameters are –
–a Shows you all files including hidden files that begin with a dot
–c Sort list by last modification of the i-node
–C Multi-column output (usually provided by default)
–d list directories only, not contents
–i print the i-node number in the first column of the report for each file
–l long list, displays most available information including permissions on a file, file size, inode number, creation date and time, owners and amount of links to the file
–L list the link references for symbolic links, rather than the link itself.
–m display the list in csv format
–p display a slash ( / ) in front of all directories.
–R display the contents of subdirectories.
–u Sort by last access instead of last modification
ls normally displays file types by colours, if it is aliased to color-ls . The standard colour scheme is
| Color | File type |
| blue | directories |
| red | compressed archives |
| white | text files |
| pink | images |
| cyan | links |
| yellow | devices |
| green | executables |
| flashing red | broken links |
ls -l provides a lot of information about a file which is confusing if you do not know how to interpret it. Typical output looks like
-rwxrw-r-- 1 ax0002 1500 Mar 06 11:26 file_name.ext
The first character, a ‘-‘ indicates that this is a file
The next 9 characters are the file permissions, broken down into 3 groups of 3 characters. The first three are the permissions for the owner, the middle three are permissions for the group which has access to the file and the last three are the permissions for everybody else.
The ‘1’ is the number of links to this file
‘ax0002’ is the file owner
‘1500’ is the file size
‘Mar 06 11:26’ is the date and time the file was last modified
‘file_name.ext’ is the file name
- mkdir
-
Creates a new directory.
Syntax:
>mkdir directory_name
You can create a directory and a sub directory below it the -p option. Say you have a directory called reports, and you want to create a path for /reports/2006/jan/ but 2006 does not exist yet. Just use
>mkdir -p reports/2006/jan/
- mv
-
used to move or just rename a file
Syntax:
>mv oldfile_name newfile_name
or
>mv oldpath\file_name newpath\file_name
- rm
-
deletes files
syntax:
>rm file_name
- rmdir
-
deletes directories
syntax:
>rmdir directory_name
- tail
-
displays the end of a file
syntax:
>tail [–f] –n filename
-n specifies the number of lines to display. –f will refresh the display as more data is added, and is handy when displaying log files
- wc
-
Counts the number of lines, words and characters in file.
Syntax:
>wc file_name
- cd
-
used to navigate to a different directory
Syntax:
>cd directoryname
- find
-
Find searches for files based on a string in the file name, but it does not look inside files. Find can also be used to find files greater than a supplied size, which can be useful if you have space problems.
Syntax:
>find path -name searchstring
Look for files in the supplied path that have the searchstring in their name
>find . -size +5000k
Look for files bigger than 5 MB
You can string commands after the find to take action on the results, for example
>find . -name "*.tmp" -exec rm {} \;
will delete all files ending in tmp. I’m NOT saying you should do this, just that it is possible.
- pwd
-
Displays the current working directory
Syntax:
>pwd
- which
-
displays the full path to command.
Syntax:
>which command
If a command is not working as expected it could be that you are picking up a different command from an earlier path in the path variable. The which command can eliminate this as a possibility.
Copyright © Lascon Storage Ltd. 2000 to present date. By entering and using this site, you accept the conditions and limitations of use