Linux Operating System 1.
6 min readFile and Directory
The “pwd” command displays the current directory:
[root@dg11 dbhome_1]# pwd
/u01/app/oracle/product/19.0.0/dbhome_1
The "ls" command lists all files and directories in the specified directory. If no location is defined it acts on the current directory. The "-a" flag lists hidden "." files. The "-l" flag lists file details.
[root@dg11 ~]# ls
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@dg11 ~]# ls /u01/app/
oracle oraInventory
[root@dg11 ~]# ls -al
total 64
dr-xr-x---. 16 root root 4096 Sep 20 18:16 .
dr-xr-xr-x. 18 root root 4096 Aug 1 00:30 ..
-rw-------. 1 root root 2175 Jul 31 23:51 anaconda-ks.cfg
More Examples:
ls -al | pg Full directory listing and prompt . ls | wc -l Count the files in the current directory. ls -alt List files in date order ls -alt | head -10 As above but only display the first 10 ls -l $ORACLE_HOME/rdbms/admin/awrrpt.sql Verify that the awrrpt.sql file has execute permissions ls -s | awk '{if ($1 > 50) print $1 " " $2 }' List all files over 50 blocks in size. ls -alq List files with hidden characters. Very useful when you cannot delete a file for an unknown reason, as sometimes a file can be created with hidden control characters. ls -lr The parameter -r shows the output in the reverse order ls -lR The -R operator makes the ls command execute recursively—that is, go under to the subdirectories and show those files too ls -ltrh or ls -larth or ls -ltrha List all files with details based on creation date with all hidden files.
The “cd” command is used to change directories:
[root@dg11 ~]# cd /u01/app/oracle
[root@dg11 oracle]# pwd
/u01/app/oracle
The “mkdir” command is used to create new directories:
-p flag tells the command to create directories/files in subdirectory.
[root@dg11 oracle]# mkdir new_directory
[root@dg11 oracle]# mkdir -p /u01/app/oracle/new_directory_1
[root@dg11 oracle]# pwd
/u01/app/oracle
[root@dg11 oracle]# ls -ltrh
total 4.0K
drwxrwxr-x. 3 oracle oinstall 20 Aug 1 00:30 product
-rw-r--r--. 1 root root 0 Sep 20 18:44 old_file
-rw-r--r--. 1 root root 0 Sep 20 18:47 old_file_backup
drwxr-xr-x. 2 root root 6 Sep 20 18:49 new_directory
drwxr-xr-x. 2 root root 6 Sep 20 18:50 new_directory_1
The “rmdir” command is used to delete directories:
[root@dg11 oracle]# rmdir new_directory_1
The “grep” command performs a search for a specified string or pattern.
Show all processes owned by oracle.
[root@dg11 oracle]# ps -eaf | grep oracle
oracle 2279 1 0 18:15 ? 00:00:00 /usr/bin/gnome-keyring-daemon --daemonize --login
oracle 2285 2268 0 18:15 ? 00:00:00 /usr/libexec/gnome-session-binary --session gnome-classic
[root@dg11 oracle]# ps -ef|grep pmon
oracle 3376 1 0 18:18 ? 00:00:00 ora_pmon_prod
root 4219 3215 0 18:53 pts/0 00:00:00 grep --color=auto pmon
The “find” command can be used to find the location of specific files. The “/” flag represents the directory for the search.
Wildcards such as “alert*.log” can be used for the filename.
[root@dg11 oracle]# find /u01 -name alert*.log
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
Display only the lines in /etc/oratab where the lines do not (-v option; negation) start with # character (^ is a special character indicating beginning of line, similarly $ is end of line).
[oracle@dg11 ~]$ grep -v '^#' /etc/oratab
prod:/u01/app/oracle/product/19.0.0/dbhome_1:N
Oracle produces many extraneous files: trace files, log files, dump files, audit files ,etc. Unless they are cleaned periodically, they can fill up the filesystem and bring the database to a halt.
To ensure that doesn’t happen, simply search for the files with extension “trc” “trm” “aud” and remove them if they are more than three days old. First command displays the list of file & below one with delete the unnecessary files.
find /u01/app/oracle/diag/rdbms/orcl/orcl/trace -name '*.trm' -mtime +3 -exec ls -ltrh {} \;
find /u02/app/oracle/diag/rdbms/orcl/orcl/trace -name '*.trm' -mtime +3 -exec rm {} \;
The “touch” command is used to create a new empty file with the default permissions:
[root@dg11 oracle]# touch new_file
[root@dg11 oracle]# ls -ltrh
total 4.0K
drwxrwxr-x. 3 oracle oinstall 20 Aug 1 00:30 product
-rw-r--r--. 1 root root 0 Sep 20 18:40 new_file
The “rm” command is used to delete files and directories. The “-R” flag tells the command to recurse through subdirectories.
[root@dg11 oracle]# rm new_file
rm: remove regular empty file ‘new_file’? y
[root@dg11 oracle]# ls -ltrh
total 4.0K
drwxrwxr-x. 3 oracle oinstall 20 Aug 1 00:30 product
drwxr-xr-x. 2 root root 6 Sep 20 18:42 directory_file
[root@dg11 oracle]# rm -rf directory_file
The “mv” command is used to move or rename files and directories. The “.” represents the current directory.
mv <existing_file_name> <new_file_name_&_path>
-rw-r--r--. 1 root root 0 Sep 20 18:44 new_file
[root@dg11 oracle]# mv new_file old_file
[root@dg11 oracle]# ls -ltrh
total 4.0K
-rw-r--r--. 1 root root 0 Sep 20 18:44 old_file
The “cp” command is used to copy files and directories:
cp <existing_file_name> <new_file_name_&_path>
[root@dg11 oracle]# cp old_file old_file_backup
[root@dg11 oracle]# ls -ltrh
total 4.0K
-rw-r--r--. 1 root root 0 Sep 20 18:44 old_file
-rw-r--r--. 1 root root 0 Sep 20 18:47 old_file_backup
The “wc” utility displays a count of the number of characters, words and lines in a file. The switches for this utility are:
-l print line count
-c print character count
-w print word count
[oracle@dg11 trace]$ wc -l prod_m000_3841.trc
420 prod_m000_3841.trc
[oracle@dg11 trace]$ wc -c prod_m000_3841.trc
19658 prod_m000_3841.trc
The “more” or “cat” commands lets you display the contents of a file:
cat <file_name>
[oracle@dg11 trace]$ cat student_review
Oracle DBA !!
[oracle@dg11 trace]$ more student_review
Oracle DBA !!
The “tail” command let you see a specified number of lines from the end of the file
10 denotes number of lines to display and -f denotes getting current stats of logfile
tail -10f <file_name>
[oracle@dg11 trace]$ tail -10f prod_m000_3841.trc
*********** START A RUN OF [KDILM background CLeaNup] *************
*** MODULE NAME:(MMON_SLAVE) 2022-09-20T19:18:32.168906+05:30
*** ACTION NAME:(Intensive AutoTask Dispatcher) 2022-09-20T19:18:32.168914+05:30
The “diff” command displays the differences between file1 and file2. Options:
-t = ignore white spaces and tabs
-i = ignore ‘case’ letters (A=a)
diff <file1_name> <file2_name>
[oracle@dg11 trace]$ diff prod_m000_3841.trm prod_tt00_3451.trm
< @8|8|nPtVce1Z2"3841|prod||1|1|1|1|9|
The “echo” command, echo strings to screen
echo $DISPLAY display the contents of the DISPLAY variable to screen.
[oracle@dg11 trace]$ echo $DISPLAY
:0
With the “du” and “df” commands, you can display hard disk information. (-k Use 1024 byte blocks instead of the default 512)
du Display disk usage for all directories and subdirectories under the current directory.
df -k Displays disk space free on each filesystem.
[oracle@dg11 oracle]$ du -sh oradata
3.3G oradata
[oracle@dg11 oracle]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs 1.4G 0 1.4G 0% /dev/shm
tmpfs 1.4G 9.5M 1.4G 1% /run
tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/mapper/ol-root 50G 26G 25G 51% /
The “ln” command let you create a link to a file.
[oracle@dg11 oracle]$ ln -s new_directory /u01/app/oracle/new_directory2022
[oracle@dg11 oracle]$ ls -ltrh
total 4.0K
drwxrwxr-x. 3 oracle oinstall 20 Aug 1 00:30 product
-rw-r--r--. 1 root root 0 Sep 20 18:44 old_file
-rw-r--r--. 1 root root 0 Sep 20 18:47 old_file_backup
drwxr-xr-x. 2 root root 6 Sep 20 18:49 new_directory
lrwxrwxrwx. 1 oracle oinstall 13 Sep 20 19:27 new_directory2022 -> new_directory
The “cksum” command provides a checksum of a file. It’s very useful for comparing two files of the same size that you suspect are different.
[oracle@dg11 oracle]$ cksum old_file_backup
4294967295 0 old_file_backup
he “gzip” and “compress” commands allows you to compress files. The gzip
command results in a compressed copy of the original file with a “.gz” extension. The gunzip
command reverses this process.
[oracle@dg11 app]$ touch new_file
[oracle@dg11 app]$ gzip new_file
[oracle@dg11 app]$ ls -ltrh
total 8.0K
drwxrwx---. 4 oracle oinstall 78 Sep 20 18:18 oraInventory
drwxrwxr-x. 11 oracle oinstall 4.0K Sep 20 19:27 oracle
-rw-r--r--. 1 oracle oinstall 29 Sep 20 19:29 new_file.gz
[oracle@dg11 app]$ gunzip new_file.gz
[oracle@dg11 app]$ ls -ltrh
total 4.0K
drwxrwx---. 4 oracle oinstall 78 Sep 20 18:18 oraInventory
drwxrwxr-x. 11 oracle oinstall 4.0K Sep 20 19:27 oracle
-rw-r--r--. 1 oracle oinstall 0 Sep 20 19:29 new_file
The “ssh” command lets you connect to a remote box. The “scp” command lets you perform remote copy operations
Run command on $HOST as $USER (default command=shell)
ssh oracle@server2.localdomain
OS version
[oracle@dg11 app]$ cat /etc/system-release
Oracle Linux Server release 7.9
[oracle@dg11 app]$ uname -r
5.4.17-2102.201.3.el7uek.x86_64
Get OS File System Block Size 64 bit
[root@dg11 ~]# uname -a
Linux dg11.localdomain 5.4.17-2102.201.3.el7uek.x86_64 #2 SMP Fri Apr 23 09:05:55 PDT 2021 x86_64 x86_64 x86_64 GNU/Linux
Hope it helped !! 🙂