0% found this document useful (0 votes)
10 views

OS Lab - Week 2

Uploaded by

earthlinggamer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

OS Lab - Week 2

Uploaded by

earthlinggamer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

OS Lab

Managing Files & Directories in


Linux & Text Editors
Week 2
01
Managing Files and
Directories
The Swiss Army Knife of Everyday Linux
File Management
Commands
As a good sysadmin, you are expected to know how to
create, delete, edit, set permissions, display, move,
copy, and determine the type of files and programs.
Tips for working with Linux

files
Hidden files aren’t really hidden—They just begin with a ., such as
the .bashrc and .bash_profile files. They are normally not visible unless
you explicitly ask for them to be displayed and aren’t deleted by
commands such as rm –f *.*.

● Filenames can contain multiple periods or no period characters—The


filenames this.is.a.long.file and thisisalongfile are perfectly reasonable
and possible.

● Spaces in filenames look nice, but are a pain to type—Use an _ or a -


instead of spaces because it’s neater and easier than prefixing all spaces
with a \. (To display a space in a filename, the system shows a space
prefixed with a backslash.)

● File extensions aren’t mandatory—But they are useful for sorting,


selection, and copy/move/delete commands, as well as for quickly
identifying a file’s type.
Command: cd
cd command in linux known as change directory command. It is used to change
current working directory.

Syntax:
cd [directory]

Different functionalities of cd command :

● cd /: this command is used to change directory to the root directory, The root
directory is the first directory in your filesystem hierarchy.

● cd dir_1/dir_2/dir_3: This command is used to move inside a directory from a


directory.

● cd ~ : this command is used to change directory to the home directory.

● cd : this command also work same as cd ~ command.

● cd .. : this command is used to move to the parent directory of current


directory, or the directory one level up from the current directory. “..”
represents parent directory.

● cd “dir name”: This command is used to navigate to a directory with white


spaces.Instead of using double quotes we can use single quotes then also this
Command: pwd
pwd stands for Print Working Directory. It prints the path of the working directory,
starting from the root.

pwd is shell built-in command(pwd) or an actual binary(/bin/pwd).

$PWD is an environment variable which stores the path of the current directory.

Syntax:
pwd
Command: ls
ls is a Linux shell command that lists directory contents of files and directories.Some
practical examples of ls command are shown below.

Syntax:
ls

Different functionalities of ls command :

● Display One File Per Line Using ls -1

● Display All Information About Files/Directories Using ls -l

● Display File Size in Human Readable Format Using ls -lh

● Order Files Based on Last Modified Time Using ls -lt

● Display Hidden Files Using ls -a (or) ls -A

● Display Files Recursively Using ls -R

● Display File UID and GID Using ls -n


Command: file
file command is used to determine the type of a file.

Syntax:
file [option] [filename]

Different options for the file command :

● -b, –brief : This is used to display just file type in brief mode.

● * option : Command displays the all files file type.

● filenames: Displays file types of multiple files

● -z option: Try to look inside compressed files.


Command: touch
The touch command is a standard command used in UNIX/Linux operating system
which is used to create, change and modify timestamps of a file.

Syntax:
touch file_name

Touch command to create multiple files: Touch command can be used to create the
multiple numbers of files at the same time. These files would be empty while
creation.

Syntax:
touch File1_name File2_name File3_name

Different options for the touch command :

● touch -c : This command is used to check whether a file is created or not. If


not created then don’t create it. This command avoids creating files.
Command: cp
cp stands for copy. This command is used to copy files or group of files or directory. It
creates an exact image of a file on a disk with different file name. cp command
require at least two filenames in its arguments.

Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

Different options for the cp command :

● -i(interactive): i stands for Interactive copying. With this option system first
warns the user before overwriting the destination file. cp prompts for a
response, if you press y then it overwrites the file and with any other option
leave it uncopied.

● -r or -R: Copying directory structure. With this option cp command shows its
recursive behavior by copying the entire directory structure recursively.

● Copying using * wildcard: The star wildcard represents anything i.e. all files
and directories. Suppose we have many text document in a directory and
wants to copy it another directory, it takes lots of time if we copy files 1 by 1 or
command becomes too long if specify all these file names as the argument,
but by using * wildcard it becomes simple.
Command: mv
mv stands for move. mv is used to move one or more files or directories from one
place to another in file system like UNIX.

Syntax:
mv [Option] source destination

Different options for the mv command :

● -i (Interactive): the -i option makes the command ask the user for
confirmation before moving a file that would overwrite an existing file, you
have to press y for confirm moving, any other key leaves the file as it is. This
option doesn’t work if the file doesn’t exist, it simply rename it or move it to
new location.
Command: mkdir
mkdir command in Linux allows the user to create directories (also referred to as
folders in some operating systems ). This command can create multiple directories at
once as well as set the permissions for the directories. It is important to note that the
user executing this command must have enough permissions to create a directory in
the parent directory, or he/she may recieve a ‘permission denied’ error.

Syntax:
mkdir [options...] [directories ...]

Different options for the mkdir command :

● -v or --verbose: It displays a message for every directory created.

● --help: It displays the help related information and exits


Command: rm
rm stands for remove here. rm command is used to remove objects such as files,
directories, symbolic links and so on from the file system like UNIX. By default, it
does not remove directories.

Syntax:
rm [OPTION]... FILE...

Different options for the rm command :

● -i (Interactive Deletion): Like in cp, the -i option makes the command ask
the user for confirmation before removing each file, you have to press y for
confirm deletion, any other key leaves the file un-deleted.

● -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-


walk and will delete all the files and sub-directories recursively of the parent
directory. At each stage it deletes everything it finds. Normally, rm wouldn’t
delete the directories but when used with this option, it will delete.

● Delete file whose name starting with a hyphen symbol (-): To remove a
file whose name begins with a dash (“-“), you can specify a double dash (“–“)
separately before the file name. This extra dash is necessary so that rm does
not misinterpret the file name as an option.
Command: locate
locate command in Linux is used to find the files by name. There is two most widely
used file searching utilities accessible to users are called find and locate. The locate
utility works better and faster than find command counterpart because instead of
searching the file system when a file search is initiated, it would look through a
database. This database contains bits and parts of files and their corresponding paths
on your system.

Syntax:
locate [OPTION]... PATTERN...

Examples:

● Search a file with specific name: locate sample.txt

● Limit Search Queries to a Specific Number: locate "*.html" -n 20

● Display The Number of Matching Entries: locate -c [.txt]*

● Ignore Case Sensitive Locate Outputs: locate -i *SAMPLE.txt*


Command: find
The find command in UNIX is a command line utility for walking a file hierarchy. It
can be used to find files and directories and perform subsequent operations on them.
It supports searching by file, folder, name, creation date, modification date, owner
and permissions.

Syntax:
find [where to start searching from] [expression determines what to find] [-options]
[what to find]

Examples:

● Search a file with specific name: find . -name sample.txt

● Search a file with pattern: find . -name *.txt

● Search for empty files and directories: find . -empty

● Search text within multiple files: find ./ -type f -name "*.txt" -exec grep
'linux' {} \;
Command: which &
whereis
which command in Linux is a command which is used to locate the executable file
associated with the given command by searching it in the path environment variable.

Syntax:
which [filename1] [filename2] ...

whereis command is used to find the location of source/binary file of a command


and manuals sections for a specified file in Linux system.

Syntax:
whereis [options] filename…
Command: tar
The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the
Archive files. tar command in Linux is one of the important command which provides
archiving functionality in Linux. We can use Linux tar command to create compressed
or uncompressed Archive files and also maintain and modify them.

Syntax:
tar [options] [archive-file] [file or directory to be archived]

Examples:

● Creating an uncompressed tar Archive using option -cvf : tar cvf file.tar
*.txt

● Extracting files from Archive using option -xvf : tar xvf file.tar

● gzip compression on the tar Archive, using option -z : tar cvzf file.tar.gz
*.txt

● Extracting a gzip tar Archive *.tar.gz using option -xvzf : tar xvzf
file.tar.gz
Questions?
02
Using Text Editors
Creating and Manipulating text files in the CLI & GUI
Using Text Editors
Although the desktop provides a beautiful graphical user interface (GUI) that’s a pleasure to work in, much
goes on outside that interface. Most Linux system configuration files are text files. Additionally, Linux gives
you the ability to create shell scripts and interact with the operations of several programs — all by using text
files.

When all is working as it should, you can edit (and even create) those files with graphical tools, but it’s highly
recommended that you also know how to edit them outside that interface, should a problem exist that keeps
the X Window System from loading. In the interface or not, you’ll be using text editors, which are programs
designed to interact with text files.
The vi editor
The vi editor (pronounced “vee eye”) is one of the oldest and most popular visual
text editors available for UNIX operating systems. Its Linux equivalent (known as vim,
which is short for “vi improved”) is therefore standard on almost every Linux
distribution. Although the vi editor is not the easiest of the editors to use when
editing text files, it has the advantage of portability.

To open an existing text file for editing, you can type vi filename (or vim filename),
where filename specifies the file to be edited.

Syntax:
vi [options] [text file]

Examples:

● Create a new text file that you will save later: vi

● Open a text file for editing : vi file.txt

● Open a text file and go to a specific line : vi +5 file.txt

● Open a text file and go to the end of the file : vi + file.txt


nano
GNU nano is a text editor for Unix-like computing systems or operating environments
using a command line interface. It emulates the Pico text editor, part of the Pine
email client, and also provides additional functionality. Unlike Pico, nano is licensed
under the GNU General Public License.

To open an existing text file for editing, you can type nano filename, where filename
specifies the file to be edited.

Syntax:
nano [options] [text file]

Examples:

● Create a new text file that you will save later: nano

● Open a text file for editing : nano file.txt

● Open a text file and go to a specific line : nano +5 file.txt

● Open a text file and go to a specific line and column : nano +5,10 file.txt
Gnome Text Editor /
gedit
gedit is the default text editor of the GNOME desktop environment and part of the
GNOME Core Applications. Designed as a general-purpose text editor, gedit
emphasizes simplicity and ease of use, with a clean and simple GUI, according to the
philosophy of the GNOME project. It can be considered GNOME’s counterpart for
Microsoft Windows’ notepad.

To open an existing text file for editing, you can type gedit filename, where filename
specifies the file to be edited.

Syntax:
gedit [options] [text file]

Examples:

● Create a new text file that you will save later: gedit

● Open a text file for editing : gedit file.txt

● Open a text file and go to a specific line : gedit +5 file.txt

● Open a text file and go to a specific line and column : gedit +5:10
file.txt
Questions?

You might also like