terminal file management commands

Useful and must-know Linux terminal commands

There are times in Linux when you don’t have a graphical user interface (GUI) and then you must use the terminal or Bash if you like. Usually, on a server is very practical to connect over the network via SSH remotely. Of course, most desktop computer distributions do have a GUI interface, and the terminal may be used more rarely than a server would require.

Nevertheless, it is crucial to know the terminal commands that I will list here. It will help you become a better Linux user, especially if you are passionate about it. I’ll do my best to categorize each batch of commands. Unfortunately, I can’t put a screenshot for each command, but I can provide a description of it with a small example.

Please note that some commands can be executed as they are, and some have extra parameters that come with the specific command. The extra parameters use a -dash character.

Navigation & File Management

pwd – Print working directory – Prints the entire directory you are in as terminal output. Very useful when you want to know where you are located when navigating.

ls – List – Lists everything inside the current directory (files & other directories) except the hidden files and directories. This command also has extra parameters like: ls -l for long format display and ls -a for displaying all, including hidden files as well.

cd – Change Directory – Moves you to another directory of your choice. It has extra parameters as well, like: cd .. for going back one directory and cd ~ for going to the home directory directly from any folder you are currently in.

mkdir – Used to create a new directory in the current location. You may create as many directories as you like with this command. Example: mkdir mynewfolder.

rmdir – Removes a directory, but this directory must be empty and not have any files inside. Example: rmdir mynewfolder.

touch – Creates a new file with or without an extension (best for .txt files) or modifies the timestamp if the file is already created. Example: touch newfile.txt (or you can use no extension at all)

cp – Copy – Copies files or directories to a new location. It works with the extra parameter -r for recursive copying. Example: cp -r file.txt Documents/ (copies the file.txt from the current directory to the Documents directory)

mv – Moves and/or renames a file or a directory in the current location or to another location. Example: mv old.txt new.txt – this is just a rename example. A move example would be mv old.txt /home/user/Documents/ – which takes the file from the current directory and moves it to the Documents directory.

rm – Removes any file you choose. Example: rm old.txt – Works with the extra parameter -r to recursively delete everything inside a directory + the directory itself afterward.

That’s it for navigation & file management. There are more commands, but they are more complex to discuss. Maybe later I will add them to a new blog space post.

 

Reading files from the terminal

cat – Displays the contents of a file directly in the terminal. Example: cat old.txt – displays the contents of that text file. Make sure the file is not too large, or it may freeze your computer due to RAM memory usage.

less – Displays a file one screen at a time and allows you to scroll through it. Example: less file.txt. To exit the less command may be tricky at first. Press the q key.

more – Displays a file page by page. Similar to cat if the text lines are short. Example: more file.txt.

head – Displays the first lines of a file. Example: head myfile.txt.

tail – Displays the last lines of a file. Example: tail myfile.txt. It’s the oppsite of head.

grep – Searches for text patterns inside files. Example: grep “error” logfile.txt.

That would be all for reading files with the terminal for now. Lastly, let’s discover the permissions commands.

 

Permissions & Ownership

chmod – Change Mode – Changes file or directory permissions. In case you need to change a file permission to read, write, or execute (r w x), example: chmod +x script.sh. Lets the script be executed as a program inside the terminal.

chown – Change owner – Changes the owner of a file or directory. Example: sudo chown username file.txt. Note that this command is executed with sudo, as it is an administrative command; sudo asks you for the admin password.

chgrp – Changes the group ownership of a file or directory. Example: sudo chgrp users file.txt, also used with sudo.

Final thoughts

Now you have the essential commands to work with when using a terminal. There are more commands out there, but there is not enough space to list them all. I will come back with a new blog post in the future and talk about other commands as well.

Please don’t hesitate to contact me via Ask Vlad! page. An email with your questions will be sent and then I can reply back with an answer.

See you next time,

Vlad

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *