Managing Files and Directories
Managing Files and Directories
directories
Directories
File and directory paths in UNIX use the forward slash "/" to separate directory names in a path.
If you are using a server your shell will start from /home/yourUserName/ directory. Also have a
look at the conventional directory layout here.
directory explanation
/ "root" directory
/usr directory usr (sub-directory of / "root" directory)
/usr/local local is a subdirectory of /usr
Creating a directory
mkdir command creates a new directory. The command below creates a new directory named
"newDir" under the current directory.
$ mkdir newDir
This command creates a new directory in user's home directory.
$ mkdir ~/newDir
The next command creates a the target directory and all the non-existing directories in the path.
The command will create samtools directory, and will create "opt" directory if it does not exist.
All of this will be done in user's home directory as indicated by "~/" in that path.
$ mkdir -p ~/opt/samtools
Type variants of these to your shell to move around your file system.
Command +
explanation
arguments
cd ~akalin
Change the current directory to the user akalin's home directory (if you have
permission).
commands explanation
ls list a directory
ls -l list a directory in detailed format including file sizes and permissions
ls -a List the current directory including hidden files. Hidden files start with "."
List all the file and directory names in the current directory using long format. Without
ls -ld * the "d" option, ls would list the contents of any sub-directory of the current. With the "d"
option, ls just lists them like regular files.
ls -lh list detailed format this time file sizes are human readable not in bytes
Finding files
There are a couple of ways you can find files in your file system. We will show
the find command, it works in the following syntax find directory -name targetfile. It is useful
when you have a rough idea about file location.
The following finds all files ending in ".html" under /home/user directory.
$ find /home/user -name "*.html"
find can also do more than just finding files. It also execute commands on the files you find via -
exec option. The following command finds all files in the current directory with ".txt" ending and
counts the number of lines in every text file. The '{}' is replaced by the name of each file found
and the ';' ends the -exec clause.
$ find . -name "*.txt" -exec wc -l '{}' ';'
Another command that can find files is locate. The locate command provides a faster way of
locating all files whose names match a particular search string. For example:
$ locate ".txt"
will find all filenames in the filesystem that contain ".txt" anywhere in their full paths.
A disadvantage of locate is that it stores all filenames on the system in an index that is usually
updated only once a day. This means locate will not find files that have been created very
recently.
grep id1
genes.txt searches and prints lines matching "id1" in "genes.txt"
grep id1 *.txt searches and prints lines matching "id1" in files ending with ".txt"
grep -vi id1 similar to above, but -i option ignores the case (Id1,ID1,iD1 and id1 treated equally),
*.txt -v option prints lines that don't match the pattern