Description | Linux Command-Line Cheat Sheet |
---|---|
Authors | christine Tranchant-Dubreuil (christine.tranchant@ird.fr) |
Creation Date | 26/02/2018 |
Last Modified Date | 20/10/2018 |
Summary
- Moving around the Filesystem and manipulating files/folders
- Displaying the contents of a file on the screen
- Searching the contents of a file
- TIPS:
- License
Moving around the Filesystem and manipulating files/folders
Printing the name/full path of the current directory pwd
Listing files in a directory ls
ls
: list all files in the current directory
Listing files in a directory gived as argument ls directory_name
ls -l
: Display the long format listing of all files in the current directory
ls -a
: Display all the files and directories (even hidden files)
Moving in the file tree cd
cd DIRECTORY_NAME
: change the current working directory to ‘directory’
change directory
Making directories mkdir directory_name
mkdir directory_name
: make a directory in your current working directory type
make directory
Copying files cp
copy
-
cp file1 file2
: makes a copy of file1 and calls it file2 -
cp file_name directory_name
: copy the file file_name to the directory _directory_name’, keeping the same name.
Moving files mv
move
mv file1 file2
: moves (or renames) file1 to file2. It is used to rename a file, by moving the file to the same directory, but giving it a different name.mv file_na me directory
: To move the file file_name from one directory to another (here directory_name). This has the effect of moving rather than copying the file, so you end up with only one file rather than two.
Be careful : use preferentially cp command rather than mv command to move big files
Removing files and directories using rm
and rmdir
remove
rm file_name
: delete (remove) a file (here file_name)
To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the science.txt file then delete it.
rmdir directory_name
: remove a directory (make sure it is empty first because linux will not let remove a non-empty directory).
Displaying the contents of a file on the screen
clear (clear screen) clear
clear
: clears the terminal. Before displaying files, it’s possible to clear the terminal window with this command.
Display the content of file using cat
cat file1
: displays the contents of a file on the screen
concatenate
cat file1 file2
cat *.fasta
Display the content of file using less
less file_name
: writes the contents of a file onto the screen a page at a time. Less is used in preference to cat for long files.
- Press the [space-bar] if you want to see another page
- Type [q] if you want to quit reading.
- still in less, type a forward slash [/] followed by the word to search
Display the begin of a file using head
head file_name
: writes the first ten lines of a file to the screen.
Display the end of a file using head
tail file_name
: writes the last ten lines of a file to the screen.
Searching the contents of a file
Searching word in a file using grep
grep motif file_name
: searches files for specified words or patterns. First clear the screen, then type
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type
Some of the other options of grep are:
-v display those lines that do NOT match -n precede each matching line with the line number -c print only the total count of matched lines
Try some of them and see the different results. Don’t forget, you can use more than one option at a time. For example, the number of lines without the words science or Science is grep -ivc science science.txt
Count for word, line count in a file using wc
wc
: short for word count
(word count)
Creating and extracting a tar gz archive using tar
- To create a tar.gz archive from a given folder
- To extract a tar.gz compressed archive
Compressing and extracting files using gzip
gzip {filename}
gzip -d {.gz file}
Knowing how much space a file or directory is using on a disk with du
du [options] [file or dir]
If you use it with no arguments you will the usage of all files and directories (recursively) of the working directory.
-h : Shows the in human readable format -s : Sumarize, so it displays the total of each subdirectory and not for its contents
Creating a file shortcut/ a symbolic link with ln -s
A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias.
ln -s source_file myfile
- source_file corresponds to the name of the existing file for which the symbolic link is created (this file can be any existing file or directory across the file systems).
- myfile is the name of the symbolic link.
Note :
- If the source file is deleted or moved it to a different location, the symbolic file will not function properly and should be either deleted or moved it.
Downloading a file over HTTP with wget
wget http://website.com/files/file.zip
: downloads the file http://website.com/files/file.zip
into the working directory.
Find files by name or extension with find
find options path motif_file
- the
path
argument defines the top level directory where find begins filtering - the
motif_file
argument gives the motif searched in the filename within the directory (and all sub-directories)
The following example search for k64 files in the current directory and all sub-directories: