Managing Files Flashcards
basic skills for managing files
- create
- delete
- move
- rename
- archive
- manipulate via scripts
Restrict non-alphanumeric characters in file naming to…
_
Special use characters for filenames
* ? / \ " `
True or False: Linux filenames are case-insensitive
False
Special filenames
- single dot (.) refers to current directory
- double dot (..) refers to parent directory
wildcard ?
stands for a single character
wildcard *
matches zero or more characters
wildcard []
match any character in the set of characters within the square brackets
wildcard range
match any character within the range
i.e. b[a-z]ck matches back, buck, and other four letter names
common file manipulation commands
- ls
- cp
- mv
- rm
- touch
command ls
syntax: ls [OPTION]… [FILE]…
- displays the names of files in a directory
ls switches
- a displays all files including dot files
- -color produces color-coded listing differentiating directories, symbolic links, files, and more
- d lists only directory name
- l long listing including information such as the file’s permission string owner, group, size, and creation date
- f appends an indicator code to end of each name to display type of file
- r displays sub-contents recursively
command cp
- copies and with wildcards
syntax: cp [OPTION]… SOURCE… DIRECTORY
cp switches
- a archive option similar to -R but preserves ownership and copies links
- f force without prompting
- i prompt before overwrite
- p preserves ownership and permissions if possible
- r copy directories recursively
- u copy only when the SOURCE file is newer than the destination file or when the destination file is missing
command mv
move - commonly used to move files and directories from one location to another or to rename them
syntax: mv [OPTION]… SOURCE… DIRECTORY
mv switches
- b back up destination file like –backup but does not accept an argument
- f do not prompt before overwriting
- i prompt before overwrite
- n do not overwrite an existing file
options which do not apply to mv
- -preserve
- -recursive
- -archive
best practice for mv command
use trailing slash (/) to avoid typos like renaming a file instead of moving to a directory
(i.e.
mv document.doc important/purchases/ moves to directory
mv document.doc important/puchases renames to purchases)
command rm
remove
syntax: rm {options] files
rm switches
- r recursive, will remove each listed directory along with contents
- d remove empty directories
- f force no prompt
note: by default rm does not remove directories.
command touch
set modification and access time to current time
syntax: touch [options] files
touch switches
- a access time
- m modification time
- c do not create file
- t specify a timestamp
common file archiving commands
- tar
- cpio
- Dd
command tar
- tape archiver
- tar [options] Archive File
(i. e. tar cvfz /media/document.tgz /my_archive) - without z, no compression
tar switches
- c create a new archive
- v verbose list of processed files
- f following is archive name
- z filter archive through gzip
tarball
files rolled up into one package
command cpio
copy files to and from archives
- cpio [options] archive
(i. e. find ./my-work | cpio -o | gzip > /media/archivename
cpio switches
- o create , copy out mode
- i extract, copy in mode
- p pass-through, combines copy-out and copy-in
command Dd
low level copying program to copy input device file to output file
- dd [options]
backs up entire partition including empty space, which means removed files still remaining on disk are copied (removing a file initially only removes pointer to that file)
dd switches
- if=inputFile
- of=outputFile
links
- links are like shortcuts, giving a file multiple identities
- hard links
- soft links (symbolic links)
command ln
manage links
syntax: ln [options] sourceFile linkName
ln switches
- f force remove any existing files that have the target link name
- i interactive prompt before replacing existing files and links
- s create symbolic link
command mkdir
syntax: mkdir [options] directoryName(s)
mkdir switches
- m mode causes new directory to have a specified permission expressed as an octal number
- p creates parent directories
rmdir
not as good as rm -r
foundations of security
- file ownership
- file permissions
two tiers of file ownership
- individual
- group
assess file ownership
ls -l
file creator gets what permission
individual owner of file
command chown
- superuser can use this to change a file’s owner
- syntax: chown [options] [newowner][:newgroup] filenames
- cannot emit both newowner and newgroup
command chgrp
- root and ordinary users can use this to change a file’s group
- regular users may only change the group to a group in which they reside
- syntax: chgrp [options] newgroup filenames
command chmod
- modify file’s permissions
- syntax: chmod [options] [mode[,mode…]] filename
- octal or symbolic modes
- octal permissions for setting an absolute permission
- symbolic permissions for making a simple change or changes to many files without affecting other permissions
command chattr
alters attributes of a file
syntax: chattr [ -RVf ] [ -v version ] [ mode ] files
set default mode and group
umask
expression of a permission as a number
- 9-bit number
- 3 base-8 numbers, each 3 bits in length
execute bit is used differently by…
- directories
- ## allows searching of directory contents
set user ID (SUID)
used along with executable files, forces linux to run program with permissions of the owner rather than permissions of the user who runs the program
set group ID (SGID)
runs a program in the context of the group of the file
execute bit is used differently by…
- directories
- allows searching of directory contents
indentifier of a SUID program
’s’ in the owner’s execute bit of the permission string (i.e. rwsr-xr-x)
identifier of a SGID program
’s’ in the group’s execute bit position in the permission string (i.e. rwxr-sr-x)
acl
access control list
- replaces group to allow us to apply multiple permissions
default group
user’s primary group
command umask
- configure default permissions
- most distributions use a default umask of 002 or 022
- umask -s produces the umask expressed symbolically
- syntax: umask [symbols] or umask [numeric expression] (i.e. umask u-x,g=r,o+w or umask 022)
chattr switches
- a sets append mode, allowing only writing that appends to document
- c compressed mode, kernal compresses written data and uncompresses when read
- i immutable mode, can’t be deleted, links to it can’t be created, and file can’t be renamed
- j data journaling for all data written to file
- s secure deletion, kernel zeros the data blocks upon deletion
linux quota system
disk space limits for individual users and groups
quota system requirements
- filesystem support
- enabling option via kernel
- modify /etc/fstab entries for every partition
- add usrquota filesystem mount to employ user quotas
- add grpquota option to use group quotas
filesystems which support quota system
- ext2fs
- ext3fs
- reiserfs
- jfs
- xfs
set a quota
- edquota
starts the Vi editor on a temporary config file (/etc/quotatab)
soft quota and grace period
warning about limit for a grace period, beyond which user is set back to their soft limit forcibly without necessarily reaching the hard quota
FHS
Filesystem Heirarchy System is a standard to define the layout of files on unix-like OSs
important directories
- /boot
- /etc
- /sbin
- /bin
- /lib
- /usr
- /usr/local
- /usr/X11r6
- /opt
- /home
- /root
- /var
- /tmp
- /mnt
- /media
- /dev
- /proc
/ directory
root
/boot directory
static and unshareable files related to the low level initial booting of the computer
/etc directory
higher level startup and config files, usually pointing to services
/bin directory
critical executable files accessible to all users and constitute the most important command to use
/bin directory
critical executable files accessible to all users and constitute the most important command to use
/sbin directory
programs normally run only by admin
/lib directory
contains program libraries
- /lib/modules contains a subset of kernel modules
/usr directory
hosts all computer programs, shareable and static, can be mounted read-only and shared with other users
/usr/local directory
- hosts files that an admin installs locally, safe from automatic software upgrades
- can be split off on own partition to protect from OS reinstallation
/usrx11r6 directory
files related to the x window system
/opt directory
like /usr, hosts readymade packages that don’t ship with the OS, static and shareable
/home directory
hosts user specific data
/root directory
home directory for root user
/var directory
hosts transient files, often put in its own partition except on user workstation
/tmp directory
- hosts temporary files created by programs
- often good to put in its own partition to easily cleanup after troubled programs
/mnt directory
mount removable media
/media directory
like /mnt but with subdirectories for specific media types
/dev directory
hosts device files
/proc directory
virtual filesystem mostly read only and used for hardware info typically
common tools for locating files
- find
- locate
- whereis
- which
- type
common tools for locating files
- find
- locate
- whereis
- which
- type
command find
- brute force
- slow but flexible and reliable
command locate
- find file by name only
- database, so faster but must be updated (updatedb)
- slocate doesn’t find files you don’t have permission to see
command whereis
- searches for files only in standard binary file directories, library directories, and man page directories
- ignores many user directories
command which
- searches your path for the command you tupe and lists the first match’s complete path
command type
shows how a file will be interpreted by the system
syntax: type [command]
hard link
- duplicate directory entries that both point to the same inode and hence to the same file
- Must reside on a single filesystem
symbolic link
- special files that point to another file or directory by name
- May point across filesystems