File System Commands in Unix: ls, cat, cp, mv, pwd, cd, mkdir, rmdir, touch, dirname, tar
This article aims to familiarize with the basic file system commands in Unix that helps to work with files and directories. Some of the commonly used File System commands in unix are ls, cat, cp, mv, pwd, cd, mkdir, rmdir, touch, dirname, tar etc.
In this article we will discuss about below Unix commands:
ls command in Unix
It is a command line utility that lists the content/files listed in a particular Unix directory.
ls command with no parameters the command displays the files listed in your current working directory. With ls command, you can add one or more modifiers to get additional information.
- List contents of a directory
- ls is a command to list computer files in Unix
ls [option] [filenames]
|Displays all files including hidden files|
|Long listing- The default output of the ls command shows only the names of the files and directories.|
The -l option shows files in a long listing format with details like file type, file permissions,Number of hard links to the file,file owner, file group, file size, Date and Time, file name.
|Displays list of directory contents with inode number|
|Lists directory contents in reverse order.|
|Sort the data by modification date|
Here is an example:
ls -l /etc/hosts
-rw-r--r-- 1 root root 317 Dec 4 11:31 /etc/hosts
cat command in Unix
The cat (“concatenate“) command is one of the most frequently used command in Unix. cat command allows us to create single or multiple files, view content of file, concatenate files and redirect output in terminal or files.
- Concatenate and display files
- It reads data from the file and gives their content as output.
cat [option...] [file ...]
$ cat filename
To view a single file.
It will show content of given filename
$ cat file1 file2
View multiple files.
Shows the content of file1 and file2.
$ cat -n filename
|Shows contents of a file preceding with line numbers|
$ cat > new1
|Create file named new1|
$ cat [sourceFile] > [destFile]
Copy the contents of one file to another file.
The content will be copied to the destination file.
$ cat -s filename
|Suppress repeated empty lines from the file.|
$ cat file1 >> file2
|Append the contents of one file to the end of another file|
$ cat "file1" "file2" "file3" > "mergedFile"
|Merge the contents of multiple files|
Examples of the cat command in Unix are:
1. Create a File with cat Command
2. Copy Content of a file to another with cat Command and view the file
cp command in Unix
Unix command used to copy files and directories
- Copy files
- It creates an exact image of a file on a disk with a different name.
cp [option] [file1] [file2]
command contains two file names, copies the contents of file1 to the file2. If the 2nd file doesn’t exist, then first it creates one and then content is copied to it.
cp [option] [file1] [file2][directory]
Command copies file1 and file2 to directory
Examples of the cat command in Unix are:
make directory dir1 using mkdir command
Copy test1 and test2 files into this directory.
Change to dir1 using cd command
Check directory dir1 content
mv command in Unix
mv command in Unix is used to move files or directories from one place to another.
- Move or rename files
- mv is used to move one or more files or directories from one place to another.
mv [option] [sourceFile] [destFile]
If the destination file doesn’t exist, it will be created.
If the destination file exist, then it will be overwrite and the source file will be deleted
pwd command in Unix
Displays the present working directory
cd command in Unix
Change Working directory or change the current working to some other folder.
Change current directory to myData
Change current directory to parent directory
Change current directory to dir1
Changes to last directory (from where you came)
Can be used to toggle between two directories
Changes to user’s home directory
Home directory is referred by ~ character
Example of cd command in Unix are:
change directory from home(~) to mydata
change directory to parent directory using cd..
Change to home directory using cd command
mkdir command in Unix
- Create new directories
mkdir [options] DirectoryNames
rmdir command in Unix
- Remove directories
rmdir [options] DirectoryNames
rm command in Unix
- Remove files or directories .For more details on this click here
rm [option...] filelist