Linuxcommands - BGIGPD/BestPractices4Pathogenomics GitHub Wiki
Basic Linux Command Lines
List the contents of the current directory
ls
which is short for listing. This command will list the contents of the current directory:
ls your_directory
Navigate to Files and Directories
Several commands are frequently used to create, inspect, rename, and delete files and directories. To start exploring them, we’ll go to our open shell window.
First, let’s find out where we are by running a command called pwd
(which stands for ‘print working directory’). Note: Directories are like places, at any time while we are using the shell, we are in exactly one place called our current working directory. Commands mostly read and write files in the current working directory, so knowing where you are before running a command is important.
Please, type pwd
and locate where you are:
pwd
Output
/home/zhaohailong
We previously showed ls
that prints the names of the files and directories in the current directory. We can make its output more comprehensible by using the -F
option which tells ls
to classify the output by adding a marker to file and directory names to indicate what they are:
- a trailing
/
indicates that this is a directory @
indicates a link*
asterisk indicates an executable
ls -F
Also we can use -l
to show more information of the file
ls -l
You can still access previous commands using up and down arrow keys to move line-by-line, or by scrolling in your terminal.
--help
option
The Most bash commands and programs that people have written to be run from within bash, support a --help
option that displays more information on how to use the command or program. ls
has lots of other options, use –-help
to find out how to use a command and what options it accepts.
ls --help
Feel free to go through the different options and play with them.
Listing in Reverse Chronological Order
By default, ls
lists the contents of a directory in alphabetical order by name. The command ls -t
lists items by time of last change instead of alphabetically. The command ls -r
lists the contents of a directory in reverse order.
Question: Which file is displayed last when you combine the -t
and -r
options? Hint: You may need to use the -l
option to see the last changed dates.
Exploring Other Directories
Not only can we use ls
on the current working directory, but we can use it to list the contents of a different directory:
ls -F yourfolder
Your output should be a list of all the files and/or sub-directories in that directory. Take a look at that directory to confirm that your output is accurate.
- First, we can look at its contents, using the same strategy as before, passing a directory name to
ls
: - Second, we can actually change our location to a different directory.
The command to change locations is cd
followed by a directory name to change our working directory. cd
stands for change directory, which is a bit misleading: the command doesn’t change the directory; it changes the shell’s current working directory. The cd
command is like double-clicking a folder in a graphical interface to get into a folder.
Let’s say we want to move to the directory we saw above. We can use the following series of commands to get there:
cd yourfolder1
cd yourfolder2
These commands will move us from our home directory into our folder1
directory, then into the folder2
directory. You will notice that cd
doesn’t print anything. This is normal. Many shell commands will not output anything to the screen when successfully executed. But if we run pwd
after it, we can see that we are now in a new directory.
We now know how to go down the directory tree (i.e. how to go into a sub-directory), but how do we go up (i.e. how do we leave a directory and go into its parent directory)? We might try the following:
Question: Try to move back to the above directory.
With our methods so far, cd
can only see sub-directories inside your current directory. There are different ways to see directories above your current location; we’ll start with the simplest.
There is a shortcut in the shell to move up one directory level that looks like this:
cd ..
.. is a special directory name meaning “the directory containing this one”, the parent of the current directory. Sure enough, if we run pwd
after running cd ..
, we’re back in the above folder:
pwd
Other Hidden Files
Use ls -a
to show hidden files
ls -a
In addition to the hidden directories ..
and .
, you may also see a file called .bash_profile
in your home directory. This file usually contains shell configuration settings. You may also see other files and directories beginning with .
These are usually files and directories that are used to configure different programs on your computer. The prefix .
is used to prevent these configuration files from cluttering the terminal when a standard ls
command is used.
These three commands are the basic commands for navigating the filesystem on your computer: pwd
, ls
, and cd
. Let’s explore some variations on those commands.
Question: What happens if you type cd
on its own, without giving a directory?
Question: How can you check what happened?
So far, when specifying directory names, or even a directory path (as above), we have been using relative paths. When you use a relative path with a command like ls
or cd
, it tries to find that location from where we are, rather than from the root of the file system.
However, it is possible to specify the absolute path to a directory by including its entire path from the root directory, which is indicated by a leading slash. The leading /
tells the computer to follow the path from the root of the file system, so it always refers to exactly one directory, no matter where we are when we run the command.
This allows us to move to our directory from anywhere on the filesystem. To find the absolute path we’re looking for, we can use pwd
and then extract the piece we need to move to object directory.
pwd
cd /home/zhaohailong/miniconda3/
Other Shortcuts
The shell interprets a tilde ~
character at the start of a path to mean “the current user’s home directory”. For example, if your home directory is /home/Username
, then ~/Downloads
is equivalent to /home/Username/Downloads
.
Another shortcut is the -
(dash) character. cd
will translate -
into the previous directory, which is faster than having to remember, then type, the full path. This is a very efficient way of moving back and forth between two directories – i.e. if you execute cd -
twice, you end up back in the starting directory.
The difference between cd ..
and cd -
is that the former brings you up, while the latter brings you back.
Try it!
Absolute vs Relative Paths
cd .
cd /
cd /home/yourname
cd ../..
cd ~
cd home
cd ~/data/..
cd
- `cd ..'
Note that the shell does most of the work through what is called tab completion. If you type the first letters of your directory name and press Tab (the tabulation key on your keyboard), the shell automatically completes the directory name for you. However, pressing Tab again does nothing, when there are multiple possibilities; pressing Tab twice brings up a list of all the files.
Working With Files and Directories
Creating directories
Now, we know how to explore files and directories, but how do we create them in the first place?
Create a directory
Let’s create a new directory called test1511
using the command mkdir test1511
:
mkdir test1511
As you might guess from its name, mkdir
means ‘make directory’. Since test1511
is a relative path (i.e., does not have a leading slash), the new directory is created in the current working directory:
ls -F
Since we’ve just created the Linux directory, there’s nothing in it yet:
ls -F test1511
Note that mkdir
is not limited to creating single directories one at a time. The -p
option allows mkdir
to create a directory with nested sub-directories in a single operation:
mkdir -p ../training/data ../training/results
The -R
option to the ls
command will list all nested sub-directories within a directory. Let’s use ls -FR
to recursivelylist the new directory hierarchy we just created in the training directory:
ls -FR ../training
Naming files and directories
Complicated names of files and directories can make your life painful when working on the command line. Here we provide a few useful tips for the names of your files and directories.
-
Don’t use spaces.
- Spaces can make a name more meaningful, but since spaces are used to separate arguments on the command line it is better to avoid them in names of files and directories. You can use
-
or_
instead (as you can see in the workshop naming style). To test this out, try typingmkdir pathogen training course
and see what directory (or directories!) are made when you check withls -F
.
# Create new directory using space in the naming mkdir pathogen training course # List directory ls -F
- Spaces can make a name more meaningful, but since spaces are used to separate arguments on the command line it is better to avoid them in names of files and directories. You can use
-
Don’t begin names with
-
(dash).- Commands treat names starting with
-
as options.
- Commands treat names starting with
-
Stick with letters, numbers,
.
(period or ‘full stop’),-
(dash) and_
(underscore).
Create a text file
Let’s change our working directory to the test folder we created using cd
, then run a text editor called vi
to create a file called example.txt
:
cd test
vi example.txt
:wq
A Different Way for Creating Files
We have seen how to create text files using the vi
editor. Now, try the following command:
touch my_file.txt
Question: Use ls -l
to inspect the files. How large is my_file.txt
?
Question: When might you want to create a file this way?
Moving files and directories
Change the file’s name using mv
, which is short for ‘move’:
mv test/example.txt test/commands.txt
The first argument tells mv
what we’re ‘moving’, while the second is where it’s to go. In this case, we’re moving test/example.txt
to test/commands.txt
, which has the same effect as renaming the file.
One must be careful when specifying the target file name, since mv
will silently overwrite any existing file with the same name, which could lead to data loss. An additional option, mv -i
(or mv – interactive
), can be used to make mv
ask you for confirmation before overwriting.
Note that mv
also works on directories.
Let’s move yourfile
into the current working directory. We use mv
once again, but this time we’ll use just the name of a directory as the second argument to tell mv
that we want to keep the filename but put the file somewhere new. (This is why the command is called ‘move’.) In this case, the directory name we use is the special directory name that we mentioned earlier.
mv test/commands.txt .
The effect is to move the file from the directory it was in to the current working directory.
ls
with a filename or directory as an argument only lists the requested file or directory. If the file given as the argument doesn’t exist, the shell returns an error as we saw above. We can use this to see that commands.txt
is now present in our current directory.
Copying files and directories
The cp
command works very much like mv
, except it copies a file instead of moving it.
cp commands.txt test/test_commands.txt
We can also copy a directory and all its contents by using the recursive option -r
, e.g. to back up a directory:
cp -r test test_backup
We can check the result by listing the contents of both the test
and test_backup
directory:
ls test test_backup
Renaming Files
Suppose that you created a plain-text file in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it: statstics.txt
Question: After creating and saving this file you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?
cp statstics.txt statistics.txt
mv statstics.txt statistics.txt
mv statstics.txt .
cp statstics.txt .
Removing files and directories
Returning to your directory, let’s tidy up this directory by removing the commands.txt
file we created. The Unix command we’ll use for this is rm
(short for ‘remove’):
rm commands.txt
We can confirm the file has gone using ls
:
ls commands.txt
The Unix shell doesn’t have a trash bin that we can recover deleted files from (though most graphical interfaces to Unix do). Instead, when we delete files, they are unlinked from the file system so that their storage space on disk can be recycled. Tools for finding and recovering deleted files do exist, but there’s no guarantee they’ll work in any particular situation, since the computer may recycle the file’s disk space right away.
Now, try to remove the test
directory using rm test
and see what will happen:
rm test
This happens because rm
by default only works on files, not directories.
rm
can remove a directory and all its contents if we use the recursive option -r
, and it will do so without any confirmation prompts:
rm -r test
Given that there is no way to retrieve files deleted using the shell, rm -r
should be used with great caution (you might consider adding the interactive option rm -r -i
).