Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

File and Directory Commands

File and Directory Commands

UNIX Command Format

In UNIX, commands consist of a single word. The second word that comes next to the Command is processed as a parameter. Example: "cd /home/user"

Options are given with the "-" sign. More than one option can be written to a command individually, or it can be written by a single "-" sign.
Example: "ls -l -a" and "ls -la" produce the same result.


The GNU version also supports long option input.
Example: "mail -h" and "mail --help" give the same output.


Option parameters are used immediately after the option with or without spaces. In the GNU version of the commands, the parameters of the long-style options are given after the "=" character after the option.

For example: "ls -w 160", "ls -w160" and "ls --width = 160" are the same. The parameter "-w" is "160".


The parameters of the commands are given after the options.
Example: "ls -w 160 directory1 directory2" The command parameters are "directory1" and "directory2".


To execute the commands at the same time on a single line, you can put a ";" between them.
Example: "configure; make; make install "


"&&" field means "and". If the command on the left is successful, the command on the right is executed, otherwise it is not.
Example: ls directory && cd directory


However, if the "ls" command lists the "directory" directory, it will run "cd directory". Any attempt to enter a non-existent directory will be blocked.

"||" field means "or". If the command on the left is successful, the command on the right is not executed.

Example: ls directory || mkdir directory

If the "ls" command can not list the "directory", the "directory" is defined with
"mkdir directory". The directory "directory" may be listed by "ls", but it will not be attempted to be defined with mkdir because the "directory" already exists.

mkdir (MaKe DIRectory) is used to create a new directory, rmdir (ReMove DIRectory) is used to delete an empty directory.
The command 'rm -rf directory_name' can be used to delete a content-rich directory with all content.


If the UNIX commands can run in error-free format, the user will not be informed that they have successfully run and finished. Feedback is given when an error occurs, only.



On older Linux systems, when running "rm -rf /" as root, all files are deleted from the system until there are no more files left. Current linux systems have been taken care of within the rm command This should be considered as a further indication of the need to work with root identity only when necessary.

Some useful commands combined with parameters:

touch : It creates a file named "file_name", if it already exists then it updates the date modified.

rm : Deletes file named "file_name".

rm -f : Deletes file named "file_name" without asking permission.

rm -i : Deletes file named "file_name" with asking permission.

rm -r : Deletes the directory and all the contents of it recursively.

rm -rf : Deletes the directory and all the contents of it recursively without asking permission.

In UNIX, there is no such thing as a file extension like Windows. A file name can have more than one dot ("."), Which is often used to make the file name more meaningful. Parts separated by periods, do not have to carry information about the format of the file.

For this reason, a file ending in ".txt" can be either an executable file or an image file.


Copy, Move and File Commands


The cp command is used to copy files and Directories. Common forms of use are:

cp : copies the file/directory specified as "source" to the destination specified as "target"

cp -r : copy the source directory and all files and directories under it to the target directory. If target does not exist, creates it.

cp -r -p : copy the source directory and all files and directories under it to the directory target. If target does not exist, creates it. In addition, copy files and directories with protection of their access rights.

Mv : Moves the file/directory specified as "source" to the location specified as "target". This command is also used to change the file/directory name (mv ).  

An existing file can be overwritten as a result of cp and mv operations. In some cases unwanted information loss may occur.

With the file command we have information about the type of file. Example:





Pattern Matching

In UNIX, pattern matching is a very useful and powerful feature. In many places it makes operations much easier.

* - replace any character string or nothing.


? - is used in place of any character. 

[] - The characters in the range are used instead of one character.

ls page * - fetch all the directories/files starting with "page" directory/file. 

ls page?.txt - fetch all directories/files starting with "page", continuing with any character and ending with ".txt". 
ls page??.txt - fetch all directories/files starting with "page", followed by any two characters and ending with ".txt".

ls page?[0-2].txt - Fetch all directories/files starting with "page", continuing with any character, continuing with 0, 1, 2 characters and ending with ".txt". 

ls page*[13579].txt - Fetch all directories/files starting with "page", followed by a single odd digit and ending with ".txt".


Example Usage of "file" Command

find /usr/include -name "stdio.h": Search for the file named "stdio.h" in the directory "/usr/include" and all directories under it.

find / home -name "*. [Ii] [Ss] [Oo]": Search all files in .iso, .iso, .iSo ... in all directories under /home.


find / -type l: search all link files from the root


find /home/user -user root: search the files owned by the root user in all directories under the /home/user directory.


find /home -uid 500: Looks for the files owned by the user with user id 500 in all directories below the /home directory.


find /home -size +2048M: Look for files larger than 2 GB in the /home directory and all directories under it.


find /home -size + 10240k: Look for files larger than 10MB in the /home directory and all directories under it.


find / -type f -mmin -90: Search for modified files in the last 90 minutes. 

find / -type f -mmin +90: Searches for files that have been "modified" before the last 90 minutes.

To read my previous article: Change of Active Identity and PAM


This post first appeared on Linux System Manual For Everyone, please read the originial post: here

Share the post

File and Directory Commands

×

Subscribe to Linux System Manual For Everyone

Get updates delivered right to your inbox!

Thank you for your subscription

×