`
If you find these tutorials useful, Please consider making a donation.

TheFrugalComputerLinuxLogo.png
TheFrugalComputerGuy.png
Command Line 3
Files and Directories







Linux Command Line (05) Touch and mkdir

We use the touch command to create files.

We can tell the difference between files and directories when using ls with the -l option

Then we go over the UNIX standard guide for naming UNIX files, they should be:
Filenames should only use lowercase a-z, 0-9, periods, underscores, and hyphens.
Always use file extensions.
Don't start your filename with a hyphen

Linux is case sensitive so myfilename.txt and Myfilename.txt are two different files on a Linux system.





 






Linux Command Line (06) nano and vi

nano and vi are text editors that are installed by default on most distributions of Linux.

Most people new to the Linux command line will find nano the easier text editor to start with.

To open a file with nano just type 'nano' space and then the file name.

Creating and editing text using nano works like the word processors we are already familiar with, we just need to type and make the edits we want to the text on the screen.

Holding down control and pressing the letter 'O' starts the write out to file process, showing us the file name it will write out (we can change the name here if we like) and then pressing enter will write out that file name.

Holding down control and pressing 'X' will exit out of nano.

To edit a file in vi - type 'vi' space and then the file name.

Press 'i' to go into 'insert mode' to insert text then press the ESC (escape key) to get out of insert mode.

You can write and quit of of vi by pressing ':wq'

In vi, use the arrow keys to move the cursor to where you want to start inserting text, the press the lower case 'i' to go into insert mode to insert text.

To get out of insert mode press the ESC (escape key).

To save a file (you need to be out of insert mode to save a file) by pressing :w





 






Linux Command Line (07) Viewing Files (cat, more, less, head, tail)

The first thing we do, is to look at how to cat out a file by typing cat and the file name.

cat means to join together so we can cat out multiple files at the same time. Adding the -n option will add line numbers to the files outputted from the cat command.

We also look at the "more" and "less"commands to see how to view files a page at a time and see why less it is better to use "less" over "more"

When we want to see the first lines at the top of a file we will use "head" command

When we want to see the last few lines of a file, we can type the "tail" command

We can follow the file file updates by using "tail -f" to see the last lines of a file, while the file is being updated. This can be handy when tailing a log file.





 






Linux Command Line (08) Output Redirection

When we type a command in the Linux Command Line, the output goes to Standard Out. By default, Standard Out points to the terminal. We can change that by adding a greater-than sign, and a filename.

Adding a greater-than sign sign and filename to a command will redirect Standard out to that filename.

Running another command using the single greater-than sign will replace what in the filename.

Using two greater-than signs before the filename will append the Standard Out text to the end of the filename.

Error messages to to Standard Error. To redirect Standard Error to a separate file add the number 2 before the greater-than sign and then add the Standard Error filename.

Output not needed should be sent to forward slash dev forward slash null

To write the Standard Out and Standard Error to the same file use an ampersand before the greater-than sign then the filename.





 






Linux Command Line (09) Moving and Renaming files and directories

Files and directories are moved with the "mv" command we would type mv then the source file then the destination file.

The source and destination can be either absolute paths or relative paths.

To move the file output.txt to a directory called Newdirectory, we would type:

mv output.txt Newdirectory

There is no rename command in the Linux Command Line, to rename a file we use the mv command with the old filename then the new filename.

Directories can be moved and renamed with the "mv" command just like files.

When moving files and directories the force option is set as the default, meaning if the destination file already exists, it will be overwritten.

Using the "n" option for no-clobber will not move the file if the destination file exists

Using the "i" for interactive option will ask to to confirm before overwriting an existing destination file...





 






Linux Command Line (10) Copy Files and Directories

The copy command works similar to the move command, the word copy is shortened down to 2 letters cp and then we type in the source and the destination

cp errorfile.txt Newdirectory

To copy to the same directory you would type cp the filename then the copy-filename

cp errorfile.txt errorfile-copy.txt

We can add options -f for force (overwrite if file exists), -n for no-clobber (Do not overwrite file if it exits), -v for verbose, and -i for interactive (Ask before overwriting the file) to the copy command

cp -vi errorfile.txt Newdirectory

We add some files to the Newdirectory2 directory with the command:
Touch Newdirectory/Newdirectory2/file{a,b,c}.txt

This will create three files filea.txt, fileb.txt, and filec.txt

When copying a directory, we must use the -r option to recursively copy everything in that directory
cp -r Newdirectory/Newdirectory2/ .

If you use the -i for interactive when copying a directory, it will ask to confirm copy file for every file in that directory (control+c with cancel you out of that)

Using the -u option will only copy files that have been updated





 






Linux Command Line (11) Delete Files and Directories

To delete a file, we use the remove command which is rm and then we type the filename we want to remove

rm errorfile.txt

We can also use the -v for verbose and the -i for interactive(Ask before deleting the file) options when removing a file

rm -vi errorfile-copy.txt

There is no trash file, so once a file is deleted, it is gone

An empty directory can be removed with the command rmdir

rmdir Emptydirectory

We add some files into a directory with the command:
Touch Newdirecotry2/file{1..5}.txt

This will create empty files file1.txt, file2.txt. file3.txt, file4.txt, and file5.txt

The rm command will remove a directory that is not empty but we need to add the -r (for remove recursively)
rm -r Newdirectory2

Adding the lower case -i option will require you to confirm deleting every file in that directory
Adding the upper case -I option will only require you to confirm once for the entire directory and everything in it