Working with File System

 Working With Files

Creating a File

touch
creates an empty file.

touch filename

Viewing File Content

cat
reads contents of file and prints it.

cat filename

Echo

echo
output/prints a string in the terminal.

echo "content"

Writing to Files Using echo Command

Using > operator we can redirect the output of echo command to a file.

echo "Hello World!" > filename

Renaming a File

  • mv
     renames the file names.
  • destination can be a new or existing file.

Syntax

mv source destination

Example:

mv practice.txt exam.txt

Copying Files

cp
copies src_file to dest_file.

Syntax

cp src_file dest_file

Example:

cp exam.txt fun_text.txt
Note :If dest_file already exist then 
cp
 overrides the contents of dest_file.

Deleting a File

rm
removes (delete) files.

Syntax

rm filename

Example

rm exam.txt

Hidden Files

  • Linux, by default, hides many of the sensitive system files, in order to avoid accidental changes.

  • Hidden files starts with "."
  • ls -a
    shows the hidden files.

  • ls -a
    also shows the current and parent directories:
    • . represents Current directory
    • .. represents parent directory

ls -a

Summary

CommandDescription
touchCreates an empty file
catReads contents of file and prints it
echoWrites text to standard output
mvRenames the file names
cpCopies content from files
rmRemoves (delete) files
ls -aShows the hidden files

Working with Directories

Creating A Directory

mkdir
creates a directory.

mkdir directory_path

Current Working Directory

pwd
prints name of current working directory.

Changing the Current Working Directory

cd
changes the current working directory.

cd directory_path
Note :
cd /
 changes your current directory to root folder.

Creating a Directory in Directory

mkdir
creates a directory.

mkdir directory_name

Switching to Parent Directory

  • cd ..
    move to parent directory.

  • Here .. is relative path to parent directory.

cd ..

File Paths

There are two notations for file paths: 1. Absolute Path 2. Relative Path

Absolute Path:

Representing the complete path of a file or folder from the root.

Relative Path:

Representing the path of a file or folder wrt. current working directory.

In relative path conventions:

  • . refers to the current working directory.

  • .. refers to the parent directory.

Home Directory

Each user in the computer is given a separate directory to work with - called home directory.

  • cd ~
     can be used to switch to home directory.
cd ~
  • cd
     (cd and space) command can also be used to switch to home directory.
cd

Renaming a directory

mv
renames the directory name

Syntax

mv source destination

Example

mv tutorial commands

Moving a directory

mv
moves files or directories from source to destination paths.

Syntax

mv source destination

Example

mv welcome.txt commands

Copying Files to Another Directory

cp
can be used to copy files between directories.

Syntax

cp file_path directory_name

Example

cp welcome.txt commands

Copying Directory

cp -r
can be used to copy a directory.

Syntax

cp -r source_path destination_path

Example

cp -r commands linux

Common Mistake

Raises an error if destination path have any non existing directories in between.

Deleting a Directory

rm -r
removes(deletes) directories.

Syntax

rm -r directory_name

Example:

rm -r commands

Summary

We can use folder/file paths for cp, mv, rm commands.

CommandDescription
mkdirCreates a directory
pwdPrints name of current working directory
cdChanges the current working directory
rm -rDeletes a directory

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form