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.

Monday, 21 August 2023

Introduction to the Linux Command Line

Linux Basics -  Introduction to the Linux Command Line

This post is an accompaniment to my YouTube series video - Intro to Linux 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.



The command line is the most powerful place in Linux and Windows. In Linux we use the Terminal to access the CLI.

The Linux CLI is case sensitive so attention to detail when entering commands is vital!

You use the CLI by providing commands and arguments - every action in a GUI is actually a bunch of commands and arguments in the background.


In the CLI:

$ prompt = standard user 

Standard User Prompt $

“#” prompt = root/super user

Super User Prompt #

Sudo - What is it and what does it mean. Sudo is the Linux equivalent of the Windows Administrator. It stands for Super User DO. It elevates the privileges of your standard account to administrator level (Super User) to perform certain tasks.

whoami = shows the current user

hostname = shows the name of the host machine

pwd = shows the current working directory - essential for knowing where in the file system you are

To update your system:

sudo apt update && sudo apt upgrade -y -  "sudo" is us asking the system to elevate our privileges to carry out the following command. "apt" is the command that we will carry out, which is the package manager for the likes of Kali and Ubuntu. "update" is the argument, what we want the command to do (to update the repositories). The double 'and' sign "&&" is chaining two commands together. Next we elevate our privileges again, run the "apt" command but this time pass the "upgrade" argument to tell the system to install the updates. Finally, we pass this command another argument of "-y" to tell the system to not prompt us before installing.

This can take anywhere from a few seconds to 15 minutes, depending on how recently the system was last upgraded!

clear or <ctrl>l = clear the screen

<ctrl>c = will break the running command

<ctrl><shift>c = copy

<ctrl><shift>v = paste

<ctrl>left arrow = move to the start of word to the left of the cursor

<ctrl>right arrow = move to the end of the word to the right of the cursor

arrow up = scroll up through command history

arrow down = scroll down through command history

history = shows all command history

!n = use the line n in the command history list (after showing the history list)

history -c = clear the command history


When starting out, use the GUI to help find your way around!


ls = shows a list of the files and directories contained in the current directory (contents of the current working directory)

ls -lah = is useful as it shows the long format, all files and folders (even hidden ones) and human-readable.

cd .. = (change directory) navigates backwards one directory - the prompt will change to reflect the new working directory

cd ../.. = will go back 2 directories

cd {directory name} = navigates into the specified directory - the prompt will change to reflect the new working directory

man {command} = shows the manual page for the specified command - useful when you need to know what the command does and the arguments it supports etc. Press q to exit the manual page.

{command} --help = (command then 2 dashes then help) will show brief info about the command and it’s usage options.


https://explainshell.com/ = for more help on commands.


In the next Blog and Video, we'll be looking at Files and Directories on the Command Line.


Thanks for reading.

Rich.

Popular Posts