Tuesday, 29 August 2023

Files and Folders in the Linux CLI

 Linux Basics - Files and Folders in the CLI

This post is an accompaniment to my YouTube series video - Files & Directories in the CLI, providing all the commands we looked at. If you're viewing this blog and have not seen the video, then take a look as we go through live demos of all the commands here.



In the last video, we looked at some basic navigation, moving in and out of Directories and listing the contents. In THIS video we’ll be looking at files and folders.

A lot of the contents in Linux are text files, or to be more accurate, files that contain text! These can range from simple text files to config files.

We (as users) need to be able to create, read and modify these type of files and that’s exactly what we’ll be looking at today.


touch {file name} - create a new file (will also modify the timestamp of a file if it already exists)

 

cp = copies a file

 

mv = moves / renames a file or directory

 

rm = removes a file (when it’s gone, it’s gone!)

 

mkdir {directory name} = creates a directory

 

rmdir  {directory name} = removes an empty directory

 

rm -rf {directory name} = removes a directory and all the files within

 

Spaces in file / directory names:

 The best practice for when a file uses multiple words is to use dashes or underscores to make things easier. If a file name has a space in it then you need to handle it slightly different on the CLI, otherwise Linux will interpret it as two (or more) different files.

Here are two ways to deal with spaces in file names on the CLI:

“file name.txt” or “directory name” = surround the whole file name (and path if using) with double or single quotes to tell the system to use all the characters as a string.

file\ name.txt or directory\ name = put a back slash (\) before the space to "escape" the space and treat it as a character in the string.

Finding files:

find . -name ‘file*.txt’ = searches in the current directory and sub directories for a file with a name starting with “file” and the extension of .txt - case sensitive.

find . -iname ‘file*.txt’ = searches in the current directory and sub directories for a file with a name starting with “file” and the extension of .txt - case insensitive.

In the next Blog and Video, we'll be looking at reading files in the CLI via a few methods. Be sure not to miss it!


Thanks for reading.

Rich.

No comments:

Post a Comment

Popular Posts