1. Home
  2. Cloud and Servers
  3. Basic file and directory management in Linux command line
  1. Home
  2. Let's Start
  3. Helpful tips
  4. Basic file and directory management in Linux command line
  1. Home
  2. Let's Start
  3. Basic file and directory management in Linux command line

Basic file and directory management in Linux command line

Linux command line (shell) is supported by Active24 for the hosting solutions as SSH or web console, in which you can work using modern web browsers like Google Chrome or Mozilla Firefox.

This guide can be used along with Linux server and cloud services.

By using web browser you will gain access to Linux command line (shell) from interface that works independently of the operating system. Access to data, files and directories, by command line is very quick and effective.

This guide describes command line that represents Bash. It is command shell interpreter programmed in the framework of the project GNU. Its name is abbreviation for Bourne again shell – it is base on Bourne Shell (bsh) which used to be most used Unix shell.

Keyboard shortcuts

At first lets present keyboard shortcuts that will speed up some operations in Linux command line significantly. These can be slightly different depending on your operating system.

ShortcutDescription
Ctrl + Shift + C
Shift + Insert
Paste copied text (Beware! Shortcut Ctrl + C will shut down the program)
TabWhile writing a command it completes rest of the letters of the command or name of the directory or file
Arrow up
Arrow down
Writes down command from the list of previously used commands (command history), but does not tart it
Ctrl + RSearch history of commands (previous commands can be displayed by command: history)
!!Starts last command (e.g. with administrator rights/roota: sudo !!)
!nStarts n-th command (n represents command number in the history of commands looked up by command: history)
Ctrl + AMoves cursor at the beginning of the line (usually key Home works the same)
Ctrl + EMoves cursor at the end of the line (usually key End works the same)
Ctrl + ZRunning program will be moved to background (same as & after command)
Alt + BMoves cursor one word back (Back)
Alt + FMoves cursor one word forward (Forward)
Ctrl + UDeletes all characters in front of cursor
Ctrl + KDeletes all characters behind cursor
Ctrl + WDeletes one word in front of cursor
Alt + DDeletes one word behind cursor
Ctrl + LDeletes whole screen (same as command clear)
Ctrl + CEnds running program (same as ending program by clicking on cross on the top right or left corner)
Ctrl + DEnds current session (same as command exit)

Creating files and directories

All commands are abbreviations or short words for description of the command. For example most used command is cd which represents “change directory” – cd.

Firstly we move to home directory by short command change directory (cd) followed by character (~). Tilde character always represents home directory at Linux command line.

cd ~

To prove that we are actually in the home directory, we can use command print working directory (pwd):

pwd

If we are in the home directory, path will be listed /home/user_name, while user_name will be replaced with currnetly logged in user e.g. John. In this directory we can use command make directory (mkdir) and touch to create 3 directories and 3 files to work with.

mkdir test
touch test/file_1.txt
mkdir test/empty-directory
mkdir test/nonempty-directory
touch test/nonempty-directory/file_2.txt
touch test/nonempty-directory/file_3.txt

By using command disk usage (du) and switch -a (meaning all – display all including files) we will list structure of directory test, that we just created:

du -a test

Until now we have used 5 commands (cd, pwd, mkdir, touch a du). Often happens that we use specific commands multiple times. That why there exists command history, which displays all previously used commands:

history

Each used character is on a new line and lines are numbered. If you want to use lastly used command, simply enter two exclamation marks (first line example). If you want to use specific command enter one exclamation mark and then add number of the line the command is on (second line example – command on the first line of the history will be executed):

!!
!1

You can search in the list of history by shortcut Ctrl + R. Press the shortcut and then enter text you wish to search for.

Sensitive information such as password may be part of the command. Command would be saved to the history with the sensitive information, so we need to erase them. To erase one item we can use switch -d (as delete, delete line 5 in the first example), or we can delete whole history by switch -c (as clear, delete all). After deleting history the changes are to be written down to a file containing the list of history (switch-w as write).

history -d 5 -w
history -c -w

Lets move to directory test, where we will continue to work with directories and files. Simply write cd t and press Tab key. Since in the current directory is only one file starting with a letter “t”, name will be completed (in our case) to “test”. Automatic completing is very useful for directories and files with long names.

cd test

Working with files and directories

We have created structure of directories and files in directory test and now we can work with it. Working with the directories and files means that we will display, search and edit their content. We will copy, move, rename and delete them. There is another guide which shows how to archive and compress files in Linux.

Before we work with files and directories we need to get an overview. Let’s see what files and directories are in the directory test available. We can do that with a command we know: du or in the current directory (test in our case) with a command ls (as list; list all files and directories). With a switch -lah we ensure detailed list (l as long) of all files and directories (even hidden with a dot at the beginning of their name; a as all) and their size is displayed in a easily readable form for humans (h as human readable).

ls -lah

Directories are usually colored differently than files. If not, you can distinguish between them by the first character: d for directories and - for files.

We have created file file_1.txt previously in this guide, where we can now place some content with a command echo and angle brackets (> and >>). One bracket (first line) means that the text content of the file will override whole content and two (second line) means that the text is added at the end and original content stays unchanged.

echo "First line in the file_1.txt" > file_1.txt
echo "Second line in the file_1.txt" >> subor_1.txt

With a help of command cat (as concatenate) we can checkt that both lines are truly in the filesubor_1.txt:

cat file_1.txt

If we would like to sort the lines alphabetically, we would need to use command cat along with a command sort. If we use command cat as a input for command sort, we will gain a alphabetically ordered list of lines in file_1.txt. Output is send to input via pipe represented with a vertical line (|):

cat file_1.txt | sort

As an input to the sorting command sort we can send the content of the file itself if we use backward angle bracket < and thus we gain the same output as in previous command, but in shorter form (without the cat):

sort < file_1.txt

It often happens that a file contains a large amout of lines. Such file can be downloaded (first line) with a command curl (as Client URL). From the downloaded file table.csv we need to display only the first 5 lines (second line; head) and last 5 lines (third line; tail).

curl -o table.csv https://file-examples-com.github.io/uploads/2017/02/file_example_CSV_5000.csv
head -n 5 table.csv
tail -n 5 table.csv

Whole content of the longer files can be read by parts with the use of command less (first line), that allows us to use Spacebar to go trough the pages and gradually display the whole document. Press key Q to end the display of the content via command less. If we need to edit the content as well, we need to first load it in the text editor (second line) like nano. Whatever we edit in the text editor we save by pressing Ctrl + X, then key Y and then press Enter.

less table.csv
nano table.csv

We have downloaded large amount of content (thousands of lines) to the file table.csv that is hart to navigate in manually. Lets say, that we would like to display only line that contain name Felisa. It can be achieved with a command grep (globally search for a regular expression and print matching lines, ie globally searches for the expression we are looking for and lists the matching lines):

grep Felisa table.csv

It is very important to create backups of meaningful regularly (first line; cp as copy). Sometimes it is appropriate to rename the backup (second line mv as move). Command mv is special because it serves for rename but also moves the file (third line). And any surplus files can be deleted fourth line; rm as remove).

cp table.csv ./unempty_directory/
mv ./unempty_directory/table.csv ./unempty_directory/table-backup.csv
mv ./unempty_directory/file_2.txt file_2.txt
rm table.csv

We have multiple files with the extension .txt so it would be nice to get some overview about how they are named. For this situation we can use command find in combination with substitute symbols like question mark (replaces one symbol with other) and wildcard (replaces any number of symbols). If we need to search for all .txt files in current directory (represented with a dot), we can use substitute symbol wildcard (first line). All files that. All files, which name starts the same and have extension .txt, but differs in one symbol (here a number) can be found in the second line.

find . -name "*.txt"
find . -name "file_?.txt"

Directories are deleted with command rm with a switch -rf. Beware, this command shall be used only if we are completely sure about what do we delete. After the run this command on the directory test in the home directory (~), the file test will be delete with all files and directories it contains without any warning:

rm -rf ~/test

Where to find additional information?

Introduced commands are shown in several examples, but they can be used for so much more. In the command line there is one very often used command man (short for manual). If you require more information about command rm, simply enter man rm and the detailed manual for command rm will show, we can navigate in it the same as for command less (with spacebar and Q key). In this way you can study any command of the Linux command line to learn every detail.

Summary

We have shown you how to create files and directories in the Linux command line. We have displayed content of files. We renamed, copied, moved and deleted files and directories. We have also shown you how to use substitute symbols, angle brackets and vertical line. To hasten the writing we have introduced some key shortcuts. At the end we have listed a great and detailed source for your study of presented but also unmentioned commands.

Updated on January 3, 2025

Was this article helpful?

Related Articles