File Management
|
Linux Filesystems: ext4 xfs |
Windows Filesystems: |
Basic Commands
|
pwd |
print working directory |
|
cd |
home directory (/home/username) or /root |
|
~ |
|
|
/ |
root directory (absolute) |
|
~ |
user directory (/home/username) |
|
. |
current directory (relative) |
|
.. |
parent directory (relative) |
|
ls |
show directory contents |
|
ls -l |
show directory contents (list view) |
|
ls -ltriah |
show directory contents (list view) -l long -t time sorted (-S for size sorted) -r reverse -i inode -a show hidden files -h human readable file sizes |
|
Type |
# of Links |
User Owner |
Group Owner |
Size |
Month |
Day |
Time |
Namedrwxr-xr-x |
|
drwxr-xr-x |
21 |
root |
root |
4096 |
Feb |
27 |
13:33 |
var |
|
lrwxrwxrwx |
1 |
root |
root |
7 |
Feb |
27 |
13:15 |
bin |
|
-rw-r--r-- |
1 |
root |
root |
0 |
Mar |
2 |
11:15 |
testfile |
|
d |
directory |
|
l |
link |
|
|
file |
Creating Files and Directories
|
touch filename |
create file |
|
rm filename |
remove file |
|
cp filename newfilename |
copy file and name it |
|
cp filename /tmp |
copy file to /tmp |
|
mv filename newfilename |
renames the file |
|
mv filename /tmp |
move file to /tmp |
|
mkdir dirname |
create directory |
|
rmdir dirname |
remove directory |
|
rm -r dirname |
remove directory |
|
chgrp groupowner filename |
change the group owner of a file |
|
chown userowner filename |
change the user owner of a file |
|
chown userowner:groupowner filename |
change the user and group owner of a file |
|
echo "text" > filename |
insert the text into the file |
|
cat filename |
display the text inside a file |
|
rm -rf |
remove directory (recursive) (force) |
Searching Files and Directories
|
nsotelo@rhel-pc:~$ find / -name "*ksh*.rpm" 2> /dev/null |
search file or directory names for items containing name ksh and ending with .rpm stderr to /dev/null |
|
grep -ri error /var/log/ 2> /dev/null | wc -l |
search file contents for key words -c count -r recursive -i ignore case sensitive -n display matched lines and their line numbers -v all except specified wc -l word count lines only |
|
egrep -i "keyword|keyword2" /var/log |
search file contents for two key words
|
|
find /etc -name "*.conf*" | xargs grep "localhost" |
combination of above commands (output of find translated and input to grep |
Soft and Hard Links
- inode - pointer of number of a file on the disk
- soft link - link will be removed if the file is removed or renamed
- hard link - deleting, renaming or moving the original file will not affect the hard link
|
ln /location/file |
creates a hard link to a file |
|
ln -s /location/file |
creates a soft link to a file |
Soft Link Example (Hard link may return an error has both directories must be on the same partition)
touch hulk
cd /tmp
/tmp $ ln -s /home/nsotelo/hulk
Input and Output Redirects (3 Redirects)
|
standard input (stdin) with file descriptor 0 |
ls >> listings |
appends the output of ls to listings file |
|
standard output (stdout) with file descriptor 1 |
cat < listings |
feeds file contents to a file |
|
standard output (stdout) with file descriptor 1 |
mail -s "office memo" allusers@abc.com < memoletter |
feeds file contents to a file |
|
standard error (stderr) with file descriptor 2 |
ls -l /root 2> errorfile |
outputs the error message to errorfile |
Pipes
Connects the output of one command directly to the input of another command
ls -ltr | more
ll | tail -l