Working With Files
Creating a File
Viewing File Content
Echo
Writing to Files Using echo Command
Using > operator we can redirect the output of echo command to a file.
Renaming a File
- mvrenames the file names.
- destination can be a new or existing file.
Syntax
Example:
Copying Files
Syntax
Example:
Deleting a File
Syntax
Example
Hidden Files
Linux, by default, hides many of the sensitive system files, in order to avoid accidental changes.
- Hidden files starts with "."
- ls -ashows the hidden files.
- ls -aalso shows the current and parent directories:
- . represents Current directory
- .. represents parent directory
Summary
Command | Description |
---|---|
touch | Creates an empty file |
cat | Reads contents of file and prints it |
echo | Writes text to standard output |
mv | Renames the file names |
cp | Copies content from files |
rm | Removes (delete) files |
ls -a | Shows the hidden files |
Working with Directories
Creating A Directory
Current Working Directory
Changing the Current Working Directory
Creating a Directory in Directory
Switching to Parent Directory
- cd ..move to parent directory.
Here .. is relative path to parent directory.
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 and space) command can also be used to switch to home directory.
Renaming a directory
Syntax
Example
Moving a directory
Syntax
Example
Copying Files to Another Directory
Syntax
Example
Copying Directory
Syntax
Example
Common Mistake
Raises an error if destination path have any non existing directories in between.
Deleting a Directory
Syntax
Example:
Summary
We can use folder/file paths for cp, mv, rm commands.
Command | Description |
---|---|
mkdir | Creates a directory |
pwd | Prints name of current working directory |
cd | Changes the current working directory |
rm -r | Deletes a directory |