DEV Community

Felice Forby
Felice Forby

Posted on

Linux Command Line Notes

Reference for basic command line commands for Linux, because I always forget a lot of these.

Show current working directory

pwd
Enter fullscreen mode Exit fullscreen mode

See username

whoami
Enter fullscreen mode Exit fullscreen mode

Changing directories

Move to root/top directory

cd /
Enter fullscreen mode Exit fullscreen mode

Move to user home directory

cd ~
Enter fullscreen mode Exit fullscreen mode

or just

cd
Enter fullscreen mode Exit fullscreen mode

Go up one directory level

cd ..
Enter fullscreen mode Exit fullscreen mode

Print to screen

echo "Hello world!"
Enter fullscreen mode Exit fullscreen mode

Creating directories and file

Create new directory

mkdir new_directory
Enter fullscreen mode Exit fullscreen mode

Create multiple directories

mkdir dir1 dir2 dir3 dir4
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create new file

touch new_file.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

View directory contents

List all directories and files in current working directory excluding hidden ones

ls
Enter fullscreen mode Exit fullscreen mode

Also show hidden files:

ls -a
Enter fullscreen mode Exit fullscreen mode

See file permissions and more info (long format)

ls -l
Enter fullscreen mode Exit fullscreen mode

Viewing files

Use cat to simply print the all the contents to the screen

cat config.txt
Enter fullscreen mode Exit fullscreen mode

This can be done with multiple file names to see contents of multiple files

cat config.txt test.txt hello.txt
Enter fullscreen mode Exit fullscreen mode

Use the less pager program to view files in an interactive pager.

less super_long_text.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

* will match zero or more characters.

For example, remove all mp3 files:

rm *.mp3
Enter fullscreen mode Exit fullscreen mode

Moving and renaming files and directories

Use mv to move or rename files

mv <file_to_move> <destination_to_move_it>
Enter fullscreen mode Exit fullscreen mode

Example: move test.txt to the testing directory one level above

mv text.txt ../testing
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example: rename test.txt to unit_tests.txt

mv text.txt unit_tests.txt
Enter fullscreen mode Exit fullscreen mode

Example: rename testing directory to unit_tests

mv testing unit_tests
Enter fullscreen mode Exit fullscreen mode

Copying file

Copy to a new directory

cp <file_to_copy> <direcory_to_copy_to>
Enter fullscreen mode Exit fullscreen mode

Example: Make a copy of config.txt in the backup directory

cp config.txt backup
Enter fullscreen mode Exit fullscreen mode

Copy a file to the same directory with a new name

cp <file_to_copy> <new_name_for_copied_file>
Enter fullscreen mode Exit fullscreen mode

Example: Create a copy of essay.txt in the current directory and name it essay_edit.txt

cp essay.txt essay_edit.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Delete a directory. This only works for empty directories.

rmdir my_folder
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Remove all directories and files in the given location (Dangerous):

rm -r folder_to_delete
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Sort command

Sort a file by line, that is, matching lines will be grouped together.

sort my_file.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

man: See documentation on a command

Get info about how to use a command and the available options with man

man ls
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
lymah profile image
Lymah

Thanks for sharing.

Collapse
 
buchslava profile image
Vyacheslav Chub

Cool! It looks like a mandatory schoolbook.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

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.

Collapse
 
morinoko profile image
Felice Forby • Edited

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.

Collapse
 
mightycoderx profile image
MightyCoderX

How long have you been studying and or using Linux?

Collapse
 
morinoko profile image
Felice Forby

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