Sunday, 19 August 2012

Creating Files in UNIX

Creating files in UNIX/LINUX

There are different  ways to creating a files two of those  i.e touch and Cat
1.Touch filename
2.cat filename
3.cat >> filename
4.>filename
5.Using vi editor

1)     $touch    filename –it  creates zero byte file size

Eg: $touch    sample

The size of the sample file is ZERO bytes.
Note: Touch do not allow you to store anything at the time of creation. It is used to crate several empty files quickly.
           Eg: $touch    s1,s2,s3 

1)     $cat  >  filename – which also used to create the files but u need to follow the CTRL+D to save the file
         Eg: $cat>sample
                ---------------
                ---------------
                ---------------
             CNTRL+D(to save and close a file)
For execution: $./filename
a)      Cat>>file name –to append the data into the file
b)     Cat  file1 file2 file 3> file 4
It would create file 4 along the contents of data of file 1, file2, file 3 
a)      Cat<file name (or) cat  filename – to view a file

Removing the files:
$rm  <option>  filename –which is used to remove the files.
OPTIONS
Remove (unlink) the FILE(s).
-d, --directory
unlink FILE, even if it is a non-empty directory (super-user only)
-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
-r, -R, --recursive
remove the contents of directories recursively
-v, --verbose
explain what is being done
--help
display this help and exit
--version
output version information and exit
To remove a file whose name starts with a `-', for example `-foo', use one of these commands:
rm -- -foo
rm ./-foo
Note that if you use rm to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred.
Note:  $rm * - it removes all file and directories in the current directory. While using this one try to check yourself. 

Creating and removing a directory
 $mkdir  dirname– which is user to create the directories and sub directories.
$mkdir –p  dir1/dir2- Here we are creating the sub  directory  dir2 in dir1.
Rmdir command. 
rmdir command will remove directory or directories if a directory is empty.
Options:
  •        rm -r directory_name will remove all files even if directory is not empty.
  •        rmdir chinnu is how you use it to remove chinnu directory.
  •        rmdir -p will remove directories and any parent directories that are empty.
  •        rmdir -s will suppress standard error messages caused by -p.
Cp command. 
cp command copies a file. If I want to copy a file named old file in a current directory to a file named newfile in a current directory. 

$cp  oldfile newfile

If  I want to copy oldfile to other directory for example /tmp then

$cp oldfile /tmp/newfile.
Useful options available with cp are -p and -r . -p options preserves the modification time and permissions, -r recursively copy a directory and its files, duplicating the tree structure.
Wc command 

wc command counts the characters, words or lines in a file depending upon the option.
Options
  •      wc -l filename will print total number of lines in a file.
  •      wc -w filename will print total number of words in a file.
  •      wc -c filename will print total number of characters in a file.
  • Listing of files:
    The ls command lists all files in the directory that match the name. If name is left blank, it will list all of the files in the directory.
    The syntax for the ls command is:
    ls [options] [names]
    Options:
    -a
    Displays all files.
    -b
    Displays nonprinting characters in octal.
    -c
    Displays files by file timestamp.
    -C
    Displays files in a columnar format (default)
    -d
    Displays only directories.
    -f
    Interprets each name as a directory, not a file.
    -F
    Flags filenames.
    -g
    Displays the long format listing, but exclude the owner name.
    -i
    Displays the inode for each file.
    -l
    Displays the long format listing.
    -L
    Displays the file or directory referenced by a symbolic link.
    -m
    Displays the names as a comma-separated list.
    -n
    Displays the long format listing, with GID and UID numbers.
    -o
    Displays the long format listing, but excludes group name.
    -p
    Displays directories with /
    -q
    Displays all nonprinting characters as ?
    -r
    Displays files in reverse order.
    -R
    Displays subdirectories as well.
    -t
    Displays newest files first. (based on timestamp)
    -u
    Displays files by the file access time.
    -x
    Displays files as rows across the screen.
    -1
    Displays each entry on a line.
    Examples:
    ls -la




No comments:

Post a Comment