0% found this document useful (0 votes)
16 views51 pages

Linux Command Basics and File Management

The document provides an overview of Linux fundamentals, including commands, shell variables, file systems, and file operations. It covers key concepts such as directory structures, file permissions, and basic commands for file and directory management. Additionally, it discusses user environment commands and file editing techniques.

Uploaded by

rifkafathima924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views51 pages

Linux Command Basics and File Management

The document provides an overview of Linux fundamentals, including commands, shell variables, file systems, and file operations. It covers key concepts such as directory structures, file permissions, and basic commands for file and directory management. Additionally, it discusses user environment commands and file editing techniques.

Uploaded by

rifkafathima924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Linux Fundamentals

ICT315
What is a command?
• A Linux command consists of a program name followed by options and
arguments
• The program name refers to a program somewhere on disk
Options usually begin with a dash, affect the behavior of the program
Arguments represent inputs and outputs
Example:
programme-name -a –b –c file-name
programme-name –abc file-name
ls -l -a /bin
ls -ls /bin
Shell Variables
• Linux can define variables and values as VARNAME=3
• To call the variable, type a dollar sign in front of the variable name
$VARNAME

•Some variables are standard and commonly defined in Linux systems


Example:
HOME, LOGNAME, PWD, SHELL, USER
File System
File System
• Every Linux file is contained in a collection called a directory
• Directories are like folders on other OS
• Directories form a hierarchy or tree
• One directory may contain other directories (subdirectories)
• The topmost directory is called the “root” directory and is denoted by
a slash (/)
Home Directories
Users’ personal files are found in home directory
Example:
/home (for normal users)
/root (for super users)

Typically the home directory looks like


/home/username
/home/rumesh
Locate to Home directory
There are different ways to locate to the home dir.

• cd command with no arguments


• cd ~
Locate to Home directory
Try these commands:
echo $HOME
pwd
cd
cd ~
echo ~
cd your-home-dir-name
System Directories
• Linux system directories contain operating system files, applications,
documentation and many more except user files which is in /user-
home directory

Naming pattern of system directories


names contain three parts called scope, category and application

/directory1/directory2/directory3
Scope
•The scope describes the purpose of entire directory hierarchy.
Example
/ root
/usr user
/usr/local locally developed system files for individuals

/usr/games/lib
Categories
•A category is the types of files found in a directory
Example
bin file category
doc file category
Categories
Categories for programs bin, lib, sbin
Categories for web files html, www, public_html, cgi_bin
Categories for documentation doc, info, man
Categories for programming src, include
Categories for hardware dev, mnt
Categories for configuration etc, init.d
Application
• The application of a directory path is basically the name of a program
like:
• /usr/local/programme
• Program might have its own subdirectory after the scope & category
• /usr/local/doc/programme

/doc contains required files


Application cont.
/boot files for booting the system.
/boot/vmlinuz

/proc currently running processes


/proc/version
/proc/ioports
/proc/uptime
/proc/self
/lost+found Damaged files rescued by a disk recovery tool
File Protections
• Linux is a multiuser system and each user can access only some file in
the system to maintain privacy and security
• That is called “Access control”
• Access control come up with main two questions
1. Who has permission?
2. Which permission is granted?
Who has permission?
• Linux files and directories have owner who has a permission to do
anything
• Owner is the person who has created the file or dir
Which permission is granted?
• Each user has permission to read, write and execute files
• Permissions also extend to directories which users may access files, create
and delete files and execute

Example:
To get the ownership & permission
ls -l file-name
ls -ld file-name
Outputs of “ls -l” & “ls –ld”
Outputs cont.

Position Meaning
1 File type:
- = file, d = directory, l = symbolic link, p = named pipe, c = character
device, b = block device
2, 3, 4 Read, write and execute permissions for file’s owner
5, 6, 7 Read, write and execute permissions for file’s group
8, 9 , 10 Read, write and execute permissions for all others
File Operations
Basic File Operations
• ls List files in a directory
• cp Copy a file
• mv Rename /move a file
• rm Delete a file
• ln Create links to a file
ls
ls [options] [file]
lists attributes of files and directories. (current or other directories)
ls options
-a List all files, including those names begin with a dot(.)
-i inode numbers of the files
-l Long listing, including file attributes
-h print file sizes in kilobytes, megabytes and gigabytes instead of bytes
-F Decorate certain filenames with meaningful symbols
“/” to directories, “*” to executables, “@” to symbolic links,
“|” to named pipes, “=” to sockets
-R If listing a directory, list its contents recursively
-d If listing a directory, do not list its contents (directory itself)
cp
cp [options] files (file|directory)

• cp command copies a file


cp file1 file2
• or copies multiple files into a directory:
cp file1 file2 file3 file4 dir
• Using the -a or -R option, can recursively copy directories
cp options
-a Copy a directory hierarchy recursively, preserving special files, permissions,
symbolic links, and hard link relationships. This combines the options -R
(recursive copy including special files), -p (permissions), and -d (links)
-p Copy not only the file contents, but also the file’s permissions, timestamps,
and if you have sufficient permission to do so, its owner and group.
-i Ask before overwriting destination files
-f Force the copy. If a destination file exists, overwrite it unconditionally
mv
mv [options] source target

• mv command can rename a file


mv file1 file2
• or move files and directories into a target directory
mv file1 file2 dir1 dir2 target_directory
• -i, -f are useful options
rm
rm [options] file-name

• rm command use to delete files


rm file1 file2 file3
Useful options
• -i
• -f : Force the deletion, ignore error or warning
• -r : Recursively remove a directory and its contents.
ln
ln [options] source target

ln command is used to create a link (reference) to another file


There are two types of links
1. A symbolic link (Soft link) refers to another file by its path
(like Windows shortcut)
2. A hard link is simply a second name for a file pointing to the same inode
ln cont.
ln [options] source target
-s Make a symbolic link.
-d Allow the super user to create a hard link to a directory
-i
-f
to find out where a symbolic link points
readlink linkname
ls -l linkname
Directory Operations
Directory operations
Command Meaning
cd Change the current directory
pwd Print the name of current working directory
mkdir Create a directory
rmdir Delete an empty directory
rm -r Delete a nonempty directory and its contents
basename Print the final part of a file path
dirname Remove the final part of a file path
Directory operations
cd [directory]
The cd command sets the current working directory
If the directory name is not supplied, cd directs to the home directory

pwd
prints the absolute path of current working directory
Directory operations cont.
basename [path]
Command prints the final component of a file path

dirname [path]
Command removes the final component of a file path
Directory operations cont.
mkdir [options] (directory name(s))
Command use to create one or more directories
mkdir dir1
mkdir a b c
Useful options
-p create parent directories automatically if not exist based on the
given path
-m create the directory with given permission
Directory operations cont.
rmdir [options] (directory name(s))
Command use to remove one or more EMPTY directories
rmdir dir1
rmdir a b c
Useful options
-r remove nonempty directories
-p remove parent directories based on the given path
Directory operations cont.
rmdir [options] (directory name(s))
Command use to remove one or more EMPTY directories
rmdir dir1
rmdir a b c
Useful options
-p remove parent directories based on the given path

use rm –r to remove non empty directories


File Creation and Editing
File Creation and Editing
There are free editors and inbuilt commands to create & editing files in
Linux
Editors example:
vim, emacs, soffice, atom, sublime
Some editors are pre installed and others can be installed

Creating empty files:


“touch” command can be used to create an empty file
touch filename
File Creation and Editing cont.
Writing data into files:
You can write data into a file by redirecting the output of a program
(using IO redirection)
example:
touch myfile
echo This is my first file > myfile
File Creation and Editing cont.
Append (>>)
You can write data into a file by appending the output of a program to
the last line of a file
example:
touch myfile
echo This is my first line> myfile
echo This is my second line >> myfile
File Creation and Editing cont.
vim Editor
“vim” is a text editor which is enhanced of the “vi” editor.

Will cover later…


File Properties
File Properties
stat Display attributes of files and directories
file Display the type of a file
wc Count lines, words and bytes in a file
du Measure the disk usage
Users and Environment
Users and Environment
logname Print login name
whoami Print current username
id Print the user ID and group membership
who, users List logged-in users
finger Print information about users
printenv Print environment details of current user
last Print the login history of users
Users and Environment cont.
Id [options] user name

Useful options
-u Print the user ID
-g Print the group ID
-G Print all group IDs to which the user belongs
-n Print names belongs to IDs
must be combined with -u, -g, or -G
Users and Environment cont.
who [options]
Command lists all logged-in users
Useful options
-H Print a row headings for the list
-l Print the hostnames of origin (for remote users)
-u Print the terminal idle time
-m Display information only about the user associated with the
current terminal
Users and Environment cont.
users [options]
Command prints names of users who have login sessions
If a user is running multiple shells, will appear multiple times
Users and Environment cont.
finger [options] user@[host]
This command prints user information and “user” argument can be a
local username or a remote user in the way of user@host
Useful options
-l Print in long format
-s Print in short format
Users and Environment cont.
printenv environment variables
This command prints all environment variables and their values
available to current shell
Users and Environment cont.
last [options] user@[host]
This command prints the history of logins of users
Useful options
-n Print only the latest N lines of output
-i Display IP addresses of hostnames
-R Don’t display hostnames

You might also like