Red Hat – Some basic commands

      No Comments on Red Hat – Some basic commands

So I learned a lot of commands today.  Some new ones and some that I just sort of thought I knew what they did.  Here we go…

cat
So I always knew what this one did.  It concatenates two files to the standard output.  What I didn’t know was that people tend to use it in a similar function to ‘more’ command.  So if one wanted to read a file you could do something like…

[[email protected] ~]# cat newfile
This is a new file
[[email protected] ~]# more newfile
This is a new file

So like I said, it seems most common to use it just to read files.  If you wanted to use it for it’s actual purpose you could do something like…

[[email protected] ~]# cat newfile newfile > biggerfile
[[email protected] ~]# more biggerfile
This is a new file
This is a new file

Now that Im thinking about this, you can use cat for standard output type things as well.  For instance, if you just wanted to redirect the standard output to a file you could go like this…

[[email protected] ~]# cat > newfile
test1
test2
[[email protected] ~]# more newfile
test1
test2

Be careful, this overwrites the file with the standard output.  So if you want to append use >> rather than >.

cp
Copying files.  Pretty straight forward.  But there are some pretty cool flags like ‘u’ that only copy newer files.

[[email protected] ~]# cp -a folder1/* folder2
[[email protected] ~]# ls -l folder2
total 12
-rw-r–r– 1 root root 43 Jul  9 20:41 file1
-rw-r–r– 1 root root 22 Jul  9 20:35 file2
-rw-r–r– 1 root root 22 Jul  9 20:36 file3

mkdir and rmdir
To straight forward to explain.  Make and remove directories.

grep
Another one that I sort of knew about but wasn’t very good at using.  Grep’ing a file allows you to search for certain pieces of information within the file or output.  So as a basic example, we could search a large file for a keyword like this…

[[email protected] ~]# more install.log | grep kernel
Installing kernel-2.6.18-194.el5.i686

Or..

[[email protected]Box ~]# grep kernel install.log
Installing kernel-2.6.18-194.el5.i686

I sort of like using the ‘pipe’ method since it’s like Cisco IOS (which Im much more comfortable with).  There are some more advanced flags you can pass to grep as well.  Those are…

c – Displays an integer of how many instance grep found in the search rather than the actual found item
i – Ignore case in the search term
n – Prefix the output with the line number.  Handy if you are looking for something in a large file that you may need to edit. 

A couple of quick examples…

[[email protected] ~]# grep -c yum install.log
4
[[email protected] ~]# grep -in YuM install.log
222:Installing yum-metadata-parser-1.1.2-3.el5.centos.i386
368:Installing yum-3.2.22-26.el5.centos.noarch
369:Installing yum-fastestmirror-1.1.16-14.el5.centos.1.noarch
375:Installing 1:yum-updatesd-0.9-2.el5.noarch

head
The head command lets you see the top part (head) of a file without loading the entire file.  By default it will show you the top 10 lines, you can change this by specifying how many lines you’d like to see…

[[email protected] ~]# head install.log
Installing setup-2.5.58-7.el5.noarch
warning: setup-2.5.58-7.el5: Header V3 DSA signature: NOKEY, key ID e8562897
Installing filesystem-2.4.0-3.el5.i386
Installing basesystem-8.0-5.1.1.el5.centos.noarch
Installing tzdata-2010e-1.el5.noarch
Installing glibc-common-2.5-49.i386
Installing cracklib-dicts-2.8.9-3.3.i386
Installing nash-5.1.19.6-61.i386
Installing rmt-0.4b41-4.el5.i386
Installing centos-release-notes-5.5-0.i386

[[email protected] ~]# head -4 install.log
Installing setup-2.5.58-7.el5.noarch
warning: setup-2.5.58-7.el5: Header V3 DSA signature: NOKEY, key ID e8562897
Installing filesystem-2.4.0-3.el5.i386
Installing basesystem-8.0-5.1.1.el5.centos.noarch

tail
Exact same deal, but shows the bottom of the file.  No examples needed on this ne I hope…

clear
Another obvious one here but it clears the terminal.  The DOS equivalent to the the ‘CLS’ command.

ls
This is the basic file list command.  If one wanted to see the contents of directory you would use the ls command.  The most common command version of this command that I use is ‘ls -l’ which shows the file details.  Some other useful flags include ‘t’ which sorts the files by modified date and ‘S’ which sorts the files by size. 

[[email protected] ~]# ls -Sl
total 56
-rw-r–r– 1 root root 16600 Jan  4  2010 rpmforge-release-0.5.1-1.el5.rf.i386.rpm
-rw-r–r– 1 root root 14498 Jun  7  2011 install.log
-rw-r–r– 1 root root  2871 Jun  7  2011 install.log.syslog
-rw——- 1 root root  1108 Jun  7  2011 anaconda-ks.cfg

ln
ln allows you to make links to other directories.  For instance, say I wanted a link in a particular folder that pointed to my apache html directory…

[[email protected] ~]# ln -s /var/www/html/
[[email protected] ~]# ls
anaconda-ks.cfg  html install.log  install.log.syslog 
[[email protected] ~]# ls -l html
lrwxrwxrwx 1 root root 14 Jul  9 20:50 html -> /var/www/html/
[[email protected] ~]# cd html
[[email protected] html]# ls -l
total 12
drwxr-xr-x 3 root   root   4096 Jun 27 21:06 interubernet
drwxr-xr-x 2 root   root   4096 Nov 25  2011 jonlangemak
drwxr-xr-x 5 apache apache 4096 Jun 13 18:26 wordpress

Notice that the ls -l of the link shows the path rather than the contents of the directory.   However, once we cd over to that link and ls -l works as we expect with specified directories.  The link can be removed like a normal file. 

So there you have it, some basic commands.  More posts coming soon on some more commands surrounding specific tasks. 

Leave a Reply

Your email address will not be published. Required fields are marked *