Basic and Navigation
Home Directory ( ~
)
On Unix and Linux, every user has a "home" directory. This directory is yours and yours alone. The location of your home directory varies depending on which OS you use. For example, on Mac OS it is /Users/<username> but on Ubuntu system, it is /home/<username>.
Finding your home directory is easy. When you first log into your system you will be placed in your home directory by default. With this knowledge, you can use pwd
command to get the path of your home directory.
$ pwd /Users/robin
Your home directory will be where your settings files get stored and where your desktop is kept. The home directory (or subdirectories under your home directory) is also where you will save documents and files that are yours and not to be shared with others.
By default, your home directory will be unreadable and inaccessible to all other users apart from yourself (and root). You can change these permissions if you want.
It is quite common to see a tilde ( ~
) in place of the full path for a home directory.
$ ~ -bash: /Users/robin: is a directory
The ~
will expand to be the path of your home directory when you use commands such as cd
.
$ cd ~ /Users/robin
Present Working Directory ( pwd
)
Understanding where you are at any given time in your file system is important. This information help you build paths to other files or help you if you get lost.
$ pwd /Users/robin/development/helloworld_linux_commands
By default, pwd
will list the “logical” path of your current directory. This means it will treat symlinked paths as if they were the actual paths. For example, my development directory doesn’t exist under my home folder,/Users/robin ; instead it is symlinked to a folder inside of/Users/robin/Dropbox .
$ pwd -P /Users/robin/Dropbox/development/helloworld_linux_commands
List Files and Directories ( ls
)
It is useful to see what files and directories in your current directory, or another directory. For this, we can use the ls
command.
$ ls includes post template index.jade run.coffee web
Listing a Different Directory ( ls <Directory>
)
To find out what files are in another directory. The ls
command allows you to specify a path to work on.
$ ls /usr/local/ CODEOFCONDUCT.md README.md lib CONTRIBUTING.md SUPPORTERS.md man Cellar bin opt LICENSE.txt etc share Library include
Listing All Files ( -a
)
It is common on Unix and Linux based machines to have "invisible" or "hidden" files that are prefixed with "."(dot). To see both "visible" and "hidden" files and directories, you can use -a
flag.
$ ls -a /usr/local/ . Cellar include .. LICENSE.txt lib .git Library man .gitignore README.md opt .yardopts SUPPORTERS.md share CODEOFCONDUCT.md bin CONTRIBUTING.md etc
Long Form Listing ( -l
)
One of the most useful flags for ls
is the -l
flag. It will list out the names of the files and directories as well as give more detailed information about them.
$ ls -l -rw-r--r-- 1 robin admin 3160 14 Apr 00:27 CODEOFCONDUCT.md -rw-r--r-- 1 robin admin 1103 14 Apr 00:27 CONTRIBUTING.md drwxr-xr-x 7 robin admin 238 14 Apr 00:28 Cellar -rw-r--r-- 1 robin admin 1241 14 Apr 00:27 LICENSE.txt drwxr-xr-x 10 robin admin 340 14 Apr 00:28 Library -> /Users/robin/Dropbox/Library -rw-r--r-- 1 robin admin 2064 14 Apr 00:27 README.md -rw-r--r-- 1 robin admin 23781 14 Apr 00:27 SUPPORTERS.md drwxrwxr-x 23 root admin 782 14 Apr 00:28 bin drwxr-xr-x 3 robin admin 102 14 Apr 00:28 etc drwxrwxr-x 30 root admin 1020 17 Mar 23:06 include drwxrwxr-x 22 root admin 748 17 Mar 23:06 lib drwxr-xr-x 5 root wheel 170 30 Mar 2013 man drwxr-xr-x 7 robin admin 238 14 Apr 00:28 opt drwxrwxr-x 10 root admin 340 14 Apr 00:28 share
If the -l
is given, the following information is displayed for each files: file mode, number of links, owner name, group name, number of bytes in the file, abbreviated month, day-of-month file last modified, hour file last modified, minutes file last modified, and the pathname. In addition, some paths are followed by -> and another path. These paths are symlinked to the directories that follow the ->.
Human Readable Sizes ( -h
)
ls -l
gives us a more detailed view of files and directories in our current directory, including the number of bytes in the file.
While knowing the number of bytes can be useful, but it more useful to see the size of the file in human readable terms, such as 1.7K or 35M. To show this information, simply use the -h
flag.
$ ls -lh -rw-r--r-- 1 bindi admin 3.1K 14 Apr 00:27 CODEOFCONDUCT.md -rw-r--r-- 1 bindi admin 1.1K 14 Apr 00:27 CONTRIBUTING.md drwxr-xr-x 7 bindi admin 238B 14 Apr 00:28 Cellar -rw-r--r-- 1 bindi admin 1.2K 14 Apr 00:27 LICENSE.txt drwxr-xr-x 10 bindi admin 340B 14 Apr 00:28 Library -rw-r--r-- 1 bindi admin 2.0K 14 Apr 00:27 README.md -rw-r--r-- 1 bindi admin 23K 14 Apr 00:27 SUPPORTERS.md drwxrwxr-x 23 root admin 782B 14 Apr 00:28 bin drwxr-xr-x 3 bindi admin 102B 14 Apr 00:28 etc drwxrwxr-x 30 root admin 1.0K 17 Mar 23:06 include drwxrwxr-x 22 root admin 748B 17 Mar 23:06 lib drwxr-xr-x 5 root wheel 170B 30 Mar 2013 man drwxr-xr-x 7 bindi admin 238B 14 Apr 00:28 opt drwxrwxr-x 10 root admin 340B 14 Apr 00:28 share
Sorting by Size ( -S
)
Another useful flag with ls
is -S
, which will sort the results by file size, instead of the default sorting by name
ls -lhS -rw-r--r-- 1 bindi admin 23K 14 Apr 00:27 SUPPORTERS.md -rw-r--r-- 1 bindi admin 3.1K 14 Apr 00:27 CODEOFCONDUCT.md -rw-r--r-- 1 bindi admin 2.0K 14 Apr 00:27 README.md -rw-r--r-- 1 bindi admin 1.2K 14 Apr 00:27 LICENSE.txt -rw-r--r-- 1 bindi admin 1.1K 14 Apr 00:27 CONTRIBUTING.md drwxrwxr-x 30 root admin 1.0K 17 Mar 23:06 include drwxrwxr-x 23 root admin 782B 14 Apr 00:28 bin drwxrwxr-x 22 root admin 748B 17 Mar 23:06 lib drwxr-xr-x 10 bindi admin 340B 14 Apr 00:28 Library drwxrwxr-x 10 root admin 340B 14 Apr 00:28 share drwxr-xr-x 7 bindi admin 238B 14 Apr 00:28 Cellar drwxr-xr-x 7 bindi admin 238B 14 Apr 00:28 opt drwxr-xr-x 5 root wheel 170B 30 Mar 2013 man drwxr-xr-x 3 bindi admin 102B 14 Apr 00:28 etc
Sorting by Last Modified Time ( -t
)
It can often be useful to sort your ls results by the last time the files were modified.
ls -lt drwxr-xr-x 7 bindi admin 238 14 Apr 00:28 Cellar drwxrwxr-x 23 root admin 782 14 Apr 00:28 bin drwxr-xr-x 7 bindi admin 238 14 Apr 00:28 opt drwxrwxr-x 10 root admin 340 14 Apr 00:28 share drwxr-xr-x 3 bindi admin 102 14 Apr 00:28 etc drwxr-xr-x 10 bindi admin 340 14 Apr 00:28 Library -rw-r--r-- 1 bindi admin 3160 14 Apr 00:27 CODEOFCONDUCT.md -rw-r--r-- 1 bindi admin 1103 14 Apr 00:27 CONTRIBUTING.md -rw-r--r-- 1 bindi admin 1241 14 Apr 00:27 LICENSE.txt -rw-r--r-- 1 bindi admin 2064 14 Apr 00:27 README.md -rw-r--r-- 1 bindi admin 23781 14 Apr 00:27 SUPPORTERS.md drwxrwxr-x 30 root admin 1020 17 Mar 23:06 include drwxrwxr-x 22 root admin 748 17 Mar 23:06 lib drwxr-xr-x 5 root wheel 170 30 Mar 2013 man
Reverse Sort ( -r
)
When you are listing a directory that contains many files, sorting with ls
is a great way to help quickly find the files or directories you are looking for. By default, ls
sorts all of its results alphabetically. Last two have shown how to sort results by size and by last modified time.
Using -r
flag we are able to reverse the result of ls
.
ls -lr drwxrwxr-x 10 root admin 340 14 Apr 00:28 share drwxr-xr-x 7 bindi admin 238 14 Apr 00:28 opt drwxr-xr-x 5 root wheel 170 30 Mar 2013 man drwxrwxr-x 22 root admin 748 17 Mar 23:06 lib drwxrwxr-x 30 root admin 1020 17 Mar 23:06 include drwxr-xr-x 3 bindi admin 102 14 Apr 00:28 etc drwxrwxr-x 23 root admin 782 14 Apr 00:28 bin -rw-r--r-- 1 bindi admin 23781 14 Apr 00:27 SUPPORTERS.md -rw-r--r-- 1 bindi admin 2064 14 Apr 00:27 README.md drwxr-xr-x 10 bindi admin 340 14 Apr 00:28 Library -rw-r--r-- 1 bindi admin 1241 14 Apr 00:27 LICENSE.txt drwxr-xr-x 7 bindi admin 238 14 Apr 00:28 Cellar -rw-r--r-- 1 bindi admin 1103 14 Apr 00:27 CONTRIBUTING.md -rw-r--r-- 1 bindi admin 3160 14 Apr 00:27 CODEOFCONDUCT.md
We can also use -r
flag with other options to reverse their sort order. For example, ls -lSr
will list the files by size with smaller files listed first.
Links ( ln
)
Links allow us to create an association from one file or directory to another. A link allows us to "point" one file or directory to another. This can be quite useful for maintaining a sane file system, or having multiple version of a file or directory, and not wanting to use disk space to store copies of those files.
An example of where links can come in handy is when deploying a web application.
Hard Links
We can use the ln
command to create a link between two files. By default, the ln
command will create a hard link between these files.
Hard links create an identical copy of the linked file on disk, that gets updated automatically as the source file gets updated. That means if the content of the source is changed, so will the target file.
To create a link to a file, we call the ln source_file target_file
. This create a link to file source_file with the name target_file .
$ ln a.txt b.txt $ ls -l -rw-r--r-- 2 robin staff 12 Dec 17 16:46 a.txt -rw-r--r-- 2 robin staff 12 Dec 17 16:46 b.txt
Above command shows the newly createdb.txt has all of the same properties as thea.txt file.
Note: An important thing about hard links is that they only work on the current file system. You can not create a hard link to a file on a different file system. To do that you need to use symbolic links.
While the content of the source and target file are linked, if the source file gets deleted, the target file will continue to live as an independent file, despite the severed link.
$ rm a.txt $ ls -l -rw-r--r-- 2 robin staff 12 Dec 17 16:46 b.txt
Above shows that the b.txt file still exists, despite the target file having been deleted.
Forcing a Link ( -f
)
If the source file already exists you will get an error
$ ln a.txt b.txt ln: b.txt: File exists
To fix this error, you can pass the -f
flag to for the link.
$ ln -f a.txt b.txt
Symbolic Links ( -s
)
The default type of link created by ln
is a hard link. The hard links do not work for directories. To create a link to a directory, you need to use the -s
flag to create a symbolic link. This can also be used for linking to files too.
In addition, the symbolic links can also link to files or directories on other file systems. This makes symbolic links (symlinks) more powerful, and more common than the default hard links.
To create a symbolic link we can use the -s
flag.
$ ln -s a.txt b.txt $ ls -l -rw-r--r-- 2 robin staff 12 Dec 17 16:46 a.txt -rw-r--r-- 1 robin staff 12 Dec 17 16:55 b.txt -> a.txt
Notice that there are two differences:
- 1: There is an arrow to show the symbolic link.
- 2: The file timestamps and size are identical in hard links, but different in symbolic links. THe reason for the difference is that with the symbolic link the operating system creates a new, small file that point to the source files.
Change Directory ( cd
)
To change, or navigate, into another directory, you need to use the cd
command.
cd
takes one argument - the path to the directory you wish to navigate to.
$ cd ~/Dropbox $ pwd /Users/robin/Dropbox
Navigating Up ( ..
)
Once inside a directory, it can be useful to be able to navigate back up a directory. One way to do this would be to call cd
giving it the full path to the directory you want to go. Another way to accomplish the task of moving up to the parenet directory is to use the special path ..
.
$ cd ..
Telling cd
to navigate to ..
will navigate to the parent directory of the current directory.
Navigating to the Home Directory
When you are in a directory and want to navigate back to your home directory, you can call cd
without any argument.
$ cd $ pwd /Users/robin
Alternative, you can cd
the ~
.
$ cd ~
Creating Directory ( mkdir
)
Directories is a great place to store and organize files, and creating them is easy. To create a directory we can use the mkdir
command followed by the name of the directory we wish to create.
$ mkdir foo $ ls -l drwxr-xr-x 2 bindi staff 68 15 Sep 23:17 foo
Create Intermediate Directories ( -p
)
The mkdir
command allows us to create nested directories using the -p
flag.
$ mkdir -p foo/bar/now $ ls -la /foo/bar/now total 0 drwxr-xr-x 2 robin staff 68 16 Sep 00:38 . drwxr-xr-x 3 robin staff 102 16 Sep 00:38 ..
Above shos the ls
command to see that now directory exists and currently empty.
Verbose Output ( -v
)
The mkdir
command supports a verbose mode flag, -v
. Adding the -v
flag will print the results of mkdir
to the console.
$ mkdir -v foo madir: created directory 'foo'
Using the -v
flag can provide useful report when writing scripts with lot of commands.
Copying Files ( cp
)
By default cp
expects file to be copied. If you try to copy a directory you will get an error.
$ cp foo bar cp: foo is a directory (not copied)
Copy a directory ( -R
)
In order to copy a directory, you need to use the -R
flag to recursively copy the directory's content to the new directory.
$ cp -Rv foo bar foo -> bar foo/1.txt -> bar/1.txt foo/2.txt -> bar/2.txt foo/3.txt -> bar/3.txt
Above command use the verbose flag , -v
, to print output on the screen. All of the files have been copied to bar .
Overwriting files ( -f
)
Supposed you have 2 files, a.txt and b.txt . If you would like to copy a.txt to b.txt , you need to use the -f
flag to force the copy to overwrite the target file.
$ cp -f a.txt b.txt $ ls -l -rw-r--r-- 1 robin staff 0 Dec 18 13:58 a.txt -rw-r--r-- 1 robin staff 0 Dec 18 14:57 b.txt
Confirm Overwriting files ( -i
)
When you are about to copy over several files at a time, and some of the target files might already exist. So it is beneficial to confirm that you are going to overwrite a file. To have this confirmation, you need to use -i
flag.
$ cp -i a.txt b.txt overwrite b.txt? (y/n)
A prompt is presented and asking you to confirm, y
or n
, whether to overwrite the target file. A y
response will overwrite the file. A n
response will skip the file and move on to the next copy.
Deleting Files ( rm
)
The rm
command is used to delete files and folders. It supports the same flags and arguments as cp
command.
$ rm -v a.txt a.txt
The -v
flag to list the deleted file.
Moving Files ( mv
)
The process of moving files is almost the same as copy files.
mv
supports the same flags as cp
. The mv
command is really just a combination of cp
and rm
to achieve the desired outcome of moving a file fomr one location to another.
$ mv -v a.txt b.txt a.txt -> b.txt
Above command achieve the same effect by using the cp
and rm
commands. This can be useful when trying to move a file across file systems, as the mv
command doesn’t support that.
Input/Output ( |
, >
)
The Unix philosophy is "do one thing, and do it well".
Redirecting Output ( |
)
With these seemingly small individual commands, we can build some pretty impressive work flows by redirecting the output of one command to the input to another command. This is made possible by using the "pipe" operator: |
.
In the following example, we "pipe" the output of the ls
command to the input of the grep
command to find all the files in my home directory that contain an underscore _
.
$ ls -a ~ | grep _ .DS_Store .bash_history .bash_profile .guard_history .pry_history .psql_history
When using the |
operator we can chain together any number of commands. For example, we can take the output of above command to thesed
command and change all of the underscores to dashes.
$ ls -a ~ | grep _ | sed "s/_/-/g" .DS-Store .bash-history .bash-profile .guard-history .pry-history .psql-history
Writing to a file ( >
)
In addition to redirecting the output from one process and sending it to another process, we can also write the output to a file using the >
operator.
$ ls -a ~ | grep _ > underscores.txt $ cat underscores.txt .DS_Store .bash_history .bash_profile .guard_history .pry_history .psql_history
Above contents of the underscores.txt
file ( using cat
), which contains the results of our search for files in the home directory that contain underscores.
Reading from a File ( <
)
We have seen the use >
to write data to a file. If we want to read data from a file, we can use <
.
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync