Reference for basic command line commands for Linux, because I always forget a lot of these.
Show current working directory
pwd
See username
whoami
Changing directories
Move to root/top directory
cd /
Move to user home directory
cd ~
or just
cd
Go up one directory level
cd ..
Print to screen
echo "Hello world!"
Creating directories and file
Create new directory
mkdir new_directory
Create multiple directories
mkdir dir1 dir2 dir3 dir4
You can't make new nested directories with mkdir
. To do so, use the -p
flag for "make parent directories"
mkdir -p new_directory/test_files/dir1
Create new file
touch new_file.txt
Use redirect to create simple files for testing etc. This will create a file if it's not already there. If there is already a file with the same name present, it will overwrite it so be careful!
echo "Hello world" > test.txt
Appending to a file
Using >
to add something to a file will overwrite what was there before. Use >>
to append to the end of file without overwriting everything.
echo "Appending some text!" >> myfile.txt
View directory contents
List all directories and files in current working directory excluding hidden ones
ls
Also show hidden files:
ls -a
See file permissions and more info (long format)
ls -l
Viewing files
Use cat
to simply print the all the contents to the screen
cat config.txt
This can be done with multiple file names to see contents of multiple files
cat config.txt test.txt hello.txt
Use the less
pager program to view files in an interactive pager.
less super_long_text.txt
Using Wildcards * and ?
?
will match any single character.
For example, if you have multiple files like test_1.txt
, test_2.txt
and test_3.txt
, you could print out all files like so:
cat test_?.txt
*
will match zero or more characters.
For example, remove all mp3
files:
rm *.mp3
Moving and renaming files and directories
Use mv
to move or rename files
mv <file_to_move> <destination_to_move_it>
Example: move test.txt
to the testing
directory one level above
mv text.txt ../testing
You can move multiple files/directories at once. List all the files/directories to move first. The last item will serve as the destination directory. The command below will move file.txt
, dir1
, and file2.txt
to the dir2
directory.
mv file.txt dir1 file2.txt dir2
Example: rename test.txt
to unit_tests.txt
mv text.txt unit_tests.txt
Example: rename testing
directory to unit_tests
mv testing unit_tests
Copying file
Copy to a new directory
cp <file_to_copy> <direcory_to_copy_to>
Example: Make a copy of config.txt
in the backup
directory
cp config.txt backup
Copy a file to the same directory with a new name
cp <file_to_copy> <new_name_for_copied_file>
Example: Create a copy of essay.txt
in the current directory and name it essay_edit.txt
cp essay.txt essay_edit.txt
Removing (Deleting) files and directories
CAUTION: These commands will permanently delete things without a way to retrieve them, so make sure you're very careful!
Delete one or more files
rm test1.txt
rm text2.txt test3.txt
Delete a directory. This only works for empty directories.
rmdir my_folder
Delete a directory and any empty sub-directories using the -p
flag. This will not delete files contained in the directories. Below would delete the my_folder
directory and any empty subfolders it contains.
rmdir -p my_folder
Remove all directories and files in the given location (Dangerous):
rm -r folder_to_delete
Word and line counts
Show line count, word count, and byte count of a file (in that order)
wc test_1.txt
> 5 10 56 test_1.txt
The above output tells us that test_1.txt
has 5 lines, 10 words, and 56 bytes.
To show only line count, use the -l
flag
wc -l test_1.txt
> 5 test_1.txt
Sort command
Sort a file by line, that is, matching lines will be grouped together.
sort my_file.txt
Piping
Pipes |
allow you to feed the output of one command into the next command.
Example: See how many files/folders there are in the user's home directory
ls ~ | wc -l
ls ~
would list all the files in the directory. Putting a pipe after it feeds the output into the wc
word count command.
Example: Count the number of unique lines in a file. Note that uniq
will give you only the unique lines in a file, but it only does so if the duplicated lines are next to each other.
sort my_file.txt | uniq | wc -l
man
: See documentation on a command
Get info about how to use a command and the available options with man
man ls
Run a command as the super user
Use sudo
followed by the command you want to run. Do this only when absolutely necessary!
Top comments (7)
Thanks for sharing.
Cool! It looks like a mandatory schoolbook.
The
touch
command is actually for updating the access and modification times of a file. The fact that it creates the file if it doesn't exist is merely a convenient side effect.Sure, I know that what it's "really" for, but most people probably use
touch
to create files, which is why I have it listed as a way to do so.How long have you been studying and or using Linux?
I've probably been using the command line for about 10 years, but I've never really committed this stuff to memory besides the most basic commands. π I just went through a Linux command line tutorial and decided to make notes on the things that it covered.
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more