Thursday, January 20, 2011

linux commands create directory & file

< Creating directories >

Creating a new, empty directory is very easy. You use the mkdir command:
$ mkdir dir1
That's it. It's really that easy!

< Removing directories >

There are two commands you can use for removing directories. If the directory is empty, you can use rmdir:
$ rmdir dir1
You can use rmdir only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. The -r option tells rm to remove a directory recursively:
$ rm -r dir1
It goes without saying that you can cause a lot of trouble with rm -r if you're not careful! In some cases it might be a good thing to use the -i option when deleting a directory with its contents so that you'd be prompted before each file in the directory gets deleted:
$ rm -ir dir1

< Copying and moving directories >

For copying and moving directories you can use the cp and mv commands just like you use them with files. Yeah, I know. If you've already tried to copy a directory with cp, you've probably noticed that cp just complains at you. Probably it says something like cp: omitting directory yadda yadda. You see, the cp command wants you to use the -r option if you want to copy a directory with its contents. The -r means "copy recursively":
$ cp -r dir1 dir2
The above creates a directory named dir2 whose contents will be identical to dir1. However, if dir2 already exists, nothing will be overwritten: the directory dir1 will be copied into the dir2 directory under the name dir2/dir1.
When renaming directories, you use the mv command exactly the same way as with files:
$ mv dir1 dir2
When dealing with directories, mv works a bit like cp does. If dir2 doesn't exist, the above will rename dir1 to dir2, but if dir2 exists, the directory dir1 will be moved into the dir2 directory under the name dir2/dir1.

Create a file in Linux using cat command.


Below is an example to create using cat command to create file.

[root@fedora ~]# cat > create-linux-file.txt
this is my file
Create file in linux using cat command
my filename is create-linux-file.txt

[root@fedora ~]# ls
anaconda-ks.cfg Desktop install.log.syslog
create-linux-file.txt install-fedora.log X.txt
[root@fedora ~]#

[root@fedora ~]# cat create-linux-file.txt
this is my file
Create file in linux using cat command
my file name is create-linux-file.txt
[root@fedora ~]#

   The command above will create a file called linux-command-list and to finish your work press Ctrl+d after the line break (press Enter key after your last line of text) to denote the end of file.  Please note that the standard symbol of redirection ' > ' (greater than) sign is necessary to create a new file.  The ls command use on above example is to verify the existence of the newly created file.

Appends text to file using cat command.


  The example below show the cat command with the appends ' >> ' redirection symbol to add more text to the file that we create earlier (create-linux-file.txt file).

[root@fedora ~]# cat >> create-linux-file.txt
this is the line appends to create-linux-file.txt
this is an example on using redirection to appends text

[root@fedora ~]# ls
[root@fedora ~]# cat create-linux-file.txt
this is my file
Create file in linux using cat command
my file name is create-linux-file.txt
this is the line appends to create-linux-file.txt
this is an example on using redirection to appends text
[root@fedora ~]#

To finish your work press Ctrl+d after the line break.  The next shell prompt suppose to appear after you press Ctrl+d key.

Display file contents on Linux system using cat command.


  There is many way to display file contents in Linux system, one of the easiest and simplest way to display the file contents is using cat command.

Note: Remember that we already create text file name create-linux-file.txt, now the procedure below show how to use cat command to display the file contents to the screen.

[root@fedora ~]# cat create-linux-file.txt
this is my file
Create file in linux using cat command
my file name is create-linux-file.txt
this is the line appends to create-linux-file.txt
this is an example on using redirection to appends text
[root@fedora ~]#

Note:
To display the file contents you don't need any redirections sign (no '>' or '>>'), just issue the cat command and the filename of file that you wish to display.
All the file contents display immediately after you issue the command.
The file contents display on the line after the command, and the file content finish before the next shell prompt.

There is many options for the cat command that you can experiment with.

[root@fedora ~]# cat -n create-linux-file.txt
    1   this is my file
    2   Create file in linux using cat command
    3   my file name is create-linux-file.txt
    4   this is the line appends to create-linux-file.txt
    5   this is an example on using redirection to appends text
[root@fedora ~]#

The cat command with the '-n' option will display the file contents with the numbered output lines on the screen.

The following are some of the flags and arguments that can be used for the cat command:

-A, --show-all   =>   equivalent to -vET
-b, --number-nonblank   =>   number nonblank output lines
-e   =>   equivalent to -vE
-E, --show-ends   =>   display $ at end of each line
-n, --number   =>   number all output lines
-s, --squeeze-blank   =>   never more than one single blank line
-t   =>   equivalent to -vT
-T, --show-tabs   =>   display TAB characters as ^I
-u   =>   (ignored)
-v, --show-nonprinting   =>   use ^ and M- notation, except for LFD and TAB
    --help   =>   display this help and exit
    --version   =>   output version information and exit

NAME:
cat - concatenate files and print on the standard output
Usage: cat [OPTION] [FILE]...

For more in formations on using cat command:
[root@fedora ~]# info cat
[root@fedora ~]# man cat
[root@fedora ~]# cat --help

No comments:

Post a Comment