Ubuntu Tutorial

File : file is used to store data or data structure.

Creating New File : to create new file touch command is used. it does not return any message on successful. syntax : touch filename

image
create new file using touch command

Hidden Files : hidden file is same as normal file but it does not display by default when listing directory content. it is not security mechanism because the access is not restricted. hidden file start with dot character (example .test.txt). file that start with dot character will be treated as hidden file. following command will list only hidden files.

  • ls -ad .*
  • ls -a | grep "^\."
  • Listing Files : ls command is used to list directory content. ls command without any parameter print files and subdirectories present in current directory.

    image
    ls command

    following command is used to print only files.

  • ls -p | grep -v /
  • ls -l | grep -v "^d"
  • find . -maxdepth 1 -type f
  • following command is used to print all files present directory including subdirectories.

  • ls -R | grep -v "^d"
  • find . -type f
  • following table shows options for ls command

    Option Description
    -a or --all print all files and directories including( hidden directories, . and .. )
    -A or --almost-all print all files and directories including( hidden directories) but not include ( . and .. )
    -c print file or directories sort by ctime (last modification of file), newest first with -lt sort by ctime with -l sort by name
    -C print output in columns (execute ls -lt then ls -ltC see difference )
    --color=auto/yes/no/always/never tells when to use color in output
    -d or --directory print directories but not their contents
    -f do not sort, enable -aU, disable -ls --color
    -F or --classify this will append indicator (one of */=>@|) to entries
    -g this is like -l, but do not list owner
    -G or --no-group don't print group names in a long listing (-l)
    -h or --human-readable print sizes in human readable format like 1kb,2mb,3gb
    --hide=PATTERN do not list implied entries matching shell PATTERN (overridden by -a or -A)
    -i or --inode print the index number of each file
    -I or --ignore=PATTERN do not list implied entries matching shell PATTERN
    -l print file and directories in long listing format
    -L or --dereference when showing file information for a symbolic link, show information for the file the link references rather than for the link itself
    -m print output with a comma separated list of entries
    -o like -l, but do not list group information
    -p or --indicator-style=slash append / indicator to directories
    -r or --reverse print reverse order while sorting
    -R or --recursive print all files and directories with subdirectories
    -s or --size print the allocated size of each file, in blocks
    -S print files and directories as sort by file size, largest first
    -t print files and directories as sort by modification time, newest first
    -u print file and directories as sort by access time, newest first
    with -lt sort by access time and with -l sort by name
    -U do not sort; list entries in directory order
    -x list entries by lines instead of by columns
    -X sort alphabetically by entry extension
    -Z or --context print any security context of each file
    -1 list one file per line. Avoid '\n' with -q or -b

    opening file in editor : vi, vim,nano or gedit are text editors to create, open and edit files. it open file as text to read or write.

  • if filename does not exits then it will create new file.
  • to exit from vi or vim use Esc and write :wq! then press enter . for nano editor press
  • ctrl + x for gedit click on close button on top or use shortcut Alt + F4
  • syntax : vi/vim/nano/gedit filename

    less command : it is used to open file in text format. it open file with special screen for user to display full file. it shows file from the first line. to use special screen use following tips.

  • To move up, down, left or right use respective arrow keys
  • To move forward by one page press spacebar
  • To move backward by one page press b or B
  • To move end of file press G
  • To move top of file press g
  • To find word press / and enter the word to search then press enter the word will get highlight
  • To exit press q
  • syntax : less filename

    image
    less command output screen

    cat command : cat command is used to print content of files on terminal.

    syntax : cat option filenames.....

    image
    cat command

    we can print multiple files at a time. we just have to pass multiple files to cat command. following command will print given 4 text files on terminal.

    example cat fil1.txt file2.txt file3.txt file4.txt

    following table shows options to use with cat command.

    Option Description
    -b or --number-nonblank print line number at the beginning of line. blank lines are not calculated, it is overridden by -n
    -E or --show-ends print $ symbol to end of the line
    -n or --number print line number at the beginning of each line
    -s or --squeeze-blank suppress repeated empty output lines if more than 1 empty line in file counted as one 1 empty line
    -T or --show-tabs if file contain tab it will display characters as ^I instead of tab

    head command : head command prints lines of file from top on terminal. by default head command print first 10 lines of file. like cat command we can pass multiple files to head command.

    syntax : head option filenames.....

    image
    head command

    following table shows options for head command.

    Option Description
    -nN or --lines=N N is number. it prints given number of top(first) lines of file
    -q or --quiet or --silent when printing multiple files do not print file names
    -v or --verbose always output headers giving file names

    tail command : tail command prints lines of file from bottom on terminal. by default tail command print last 10 lines of file. we can pass multiple files to tail command.

    syntax : tail option filenames.....

    image
    tail command

    following table shows options for tail command.

    Option Description
    -F it place watch on file. if file content changes it show
    changes made in file on terminal output. to cancel use ctrl + c
    -nN or --lines=N N is number. it prints given number of bottom(last) lines of file
    -q or --quiet or --silent when printing multiple files do not print file names
    --retry keep trying to open a file if it is inaccessible
    -v or --verbose always output headers giving file names

    wc command : wc command is used to print count of characters, words and lines in files.

    syntax : wc option filenames....

    image
    wc command

    following are option for wc command

    Option Description
    -m or --chars print the total character count of files
    -l or --lines print the count of lines in files
    -L or --max-line-length print length of of line having maximum length in file
    -w or --words print the total count of words in files