This article deals with so-called "core" utilities on a GNU/Linux system, such as '''less''', '''ls''', and '''grep'''. The scope of this article includes -- ''but is not limited to'' -- those utilities included with the GNU {{Pkg|coreutils}} package. What follows are various tips and tricks and other helpful information related to these utilities. If sections grow too detailed, please split into separate articles.

+

This article deals with so-called "core" utilities on a GNU/Linux system, such as '''less''', '''ls''', and '''grep'''. The scope of this article includes - ''but is not limited to'' - those utilities included with the GNU {{Pkg|coreutils}} package. What follows are various tips and tricks and other helpful information related to these utilities. If sections grow too detailed, please split into separate articles.

+

+

== cat ==

+

+

[[Wikipedia:cat_(Unix)|cat]] (''catenate'') is a standard Unix utility that concatenates and lists files.

+

+

* As '''cat''' is not a shell built-in, in many occasions you may find more convenient to use a redirection, for example in scripts, or if you care a lot about performance. In fact {{ic|$ < ''file''}} does the same of {{ic|$ cat ''file''}}.

+

+

* To append multiple lines to a file a construct like this one is used:

+

+

cat << EOF >> ''path/file''

+

''first line''

+

...

+

''last line''

+

EOF

+

+

* If you need to cat file lines in reverse order, there is a utility called '''tac''' (''cat'' reversed).

== grep ==

== grep ==

−

[[Wikipedia:grep|grep]] is a command line text search utility originally written for Unix. The '''grep''' command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output. Beyond aesthetics, '''grep'''&#39;s color output is immensely useful for learning '''regexp''' and '''grep'''&#39;s functionality.

+

[[Wikipedia:grep|grep]] (from [[Wikipedia:ed|ed]]'s ''g/re/p'', ''global / regular expression / print'') is a command line text search utility originally written for Unix. The '''grep''' command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.

To use the default colors for '''grep''', write the following entry to your shell configuration file, e.g. if using [[Bash]]:

To use the default colors for '''grep''', write the following entry to your shell configuration file, e.g. if using [[Bash]]:

{{hc|~/.bashrc|2=alias grep='grep --color=auto'}}

{{hc|~/.bashrc|2=alias grep='grep --color=auto'}}

−

Alternatively, you can set the '''GREP_OPTIONS''' [[Environment Variables|environment variable]] [http://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html] bearing in mind this may break some scripts that use '''grep''' [http://brainstorm.ubuntu.com/idea/24141/]:

+

Alternatively, you can set the '''GREP_OPTIONS''' [[Environment Variables|environment variable]] [http://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html] bearing in mind this may break some scripts that use '''grep''' [http://brainstorm.ubuntu.com/idea/24141/]:

export GREP_OPTIONS='--color=auto'

export GREP_OPTIONS='--color=auto'

To include file line numbers in the output, add "''-n''":

To include file line numbers in the output, add "''-n''":

−

alias grep='grep -n --color=auto'

+

alias grep='grep -n --color=auto'

The environment variable '''GREP_COLORS''' may be used to specify different colors than the defaults.

The environment variable '''GREP_COLORS''' may be used to specify different colors than the defaults.

+

+

== ifconfig ==

+

+

[[Wikipedia:ifconfig|ifconfig]] (''interface configuration'') is a system administration utility in Unix-like operating systems to configure, control, and query TCP/IP network interface parameters from a command line interface (CLI) or in system configuration scripts.

+

+

It is deprecated: the alternative is called [[Configuring_Network|ip]].

== less ==

== less ==

Line 59:

Line 90:

Users may now list the compressed files inside of an archive using their pager:

Users may now list the compressed files inside of an archive using their pager:

'''Vim''' has a script to view the content of text files, compressed files, binaries, directories. Add the following line to your shell configuration file:

+

[[Vim]] (''visual editor improved'') has a script to view the content of text files, compressed files, binaries, directories. Add the following line to your shell configuration file:

{{hc|~/.bashrc|2=alias less='/usr/share/vim/vim73/macros/less.sh'}}

{{hc|~/.bashrc|2=alias less='/usr/share/vim/vim73/macros/less.sh'}}

== ls ==

== ls ==

−

[[Wikipedia:ls|ls]] is a command to list files in Unix and Unix-like operating systems.

+

[[Wikipedia:ls|ls]] (''list'') is a command to list files in Unix and Unix-like operating systems.

Colored output can be enabled with a simple alias. File {{ic|~/.bashrc}} should already have the following entry copied from {{ic|/etc/skel/.bashrc}}:

Colored output can be enabled with a simple alias. File {{ic|~/.bashrc}} should already have the following entry copied from {{ic|/etc/skel/.bashrc}}:

alias ls='ls --color=auto'

alias ls='ls --color=auto'

−

The next step will further enhance the colored '''ls''' output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to your shell configuration file:

The next step will further enhance the colored '''ls''' output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to your shell configuration file:

{{hc|~/.bashrc|eval $(dircolors -b)}}

{{hc|~/.bashrc|eval $(dircolors -b)}}

+

+

== man ==

+

+

[[Wikipedia:Man page|man]] (''manual page'') is a form of online software documentation usually found on a Unix or Unix-like operating system. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts. See [[Man Pages]].

+

+

== mkdir ==

+

+

[[Wikipedia:mkdir_(Unix)|mkdir]] (''make directory'') is a command to create directories. To create a directory and its whole hierarchy, ''-p'' switch is used, if not a error is printed. As users are supposed to know what they want, ''-p'' switch may be used as a default.

+

alias mkdir='mkdir -p -v'

+

The ''-v'' switch make it verbose.

+

{{Note|If you want just a temporary directory a better alternative may be '''mktemp''' (''make termporary''): {{ic|$ mktemp -p}}.

+

}}

+

+

== mv ==

+

+

[[Wikipedia:mv_(Unix)|mv]] (''move'') is a command to move and rename files and directories. It could be very dangerous so it is prudent to limit its scope:

+

alias mv=' timeout 8 mv -iv'

+

This alias suspends '''mv''' after eight seconds, asks confirmation to delete three or more files, lists the operations in progress and does not store itself in the shell history file if the shell is configured to ignore space starting commands.

== rm ==

== rm ==

−

[[Wikipedia:rm_(Unix)|rm]] is a command to delete files and directories.

+

[[Wikipedia:rm_(Unix)|rm]] (''remove'') is a command to delete files and directories. It could be very dangerous so it is prudent to limit its scope:

−

It could be very dangerous so it is prudent to limit its scope:

+

alias rm=' timeout 3 rm -Iv --one-file-system'

−

alias rm=' timeout 3 rm -Iv'

+

This alias suspends '''rm''' after three seconds, asks confirmation to delete three or more files, lists the operations in progress, does not involve more than one file systems and does not store itself in the shell history file if the shell is configured to ignore space starting commands. Substitute {{ic|-I}} with {{ic|-i}} if you prefer to confirm even for one file.

−

This alias suspends '''rm''' after three seconds, asks confirmation to delete three or more files, lists the operations in progress and does not store itself in the shell history file if the shell is configured to ignore space starting commands.

+

+

== sed ==

+

+

[[Wikipedia:sed|sed]] (''stream editor'') is a Unix utility that parses and transforms text.

+

+

Here is a handy [http://sed.sourceforge.net/sed1line.txt list] of '''sed''' one-liners examples.

+

+

{{Note|More powerful alternatives are awk and even Perl language.}}

+

+

== seq ==

+

+

'''seq''' (''sequence'') is a utility for generating a sequence of numbers. Shell built-in alternatives are available, so it is good practice to use them as explained on [[Wikipedia:seq|Wikipedia]].

+

+

== shred ==

+

+

[[Wikipedia:Shred_(Unix)|shred]] is a command to securely delete files and directories. It could be very dangerous so it is prudent to limit its scope:

+

alias shred=' timeout 3 shred -v'

+

This alias suspends '''shred''' after three seconds, lists the operations in progress, and does not store itself in the shell history file if the shell is configured to ignore space starting commands.

+

+

== sudo ==

−

A similar solution may be used for '''shred'''.

+

[[Wikipedia:Sudo|Sudo]] (''as superuser do'') is a program for Unix-like computer operating systems that allows users to run programs with the security privileges of another user (normally the superuser, or root). See [[Sudo]].

== See also ==

== See also ==

* [http://www.reddit.com/r/commandline/comments/19garq/a_sampling_of_coreutils_120/ A sampling of coreutils] [http://www.reddit.com/r/commandline/comments/19ge6v/a_sampling_of_coreutils_2040/ , part 2] [http://www.reddit.com/r/commandline/comments/19j1w3/a_sampling_of_coreutils_4060/ , part 3] - An overview of commands in coreutils

* [http://www.reddit.com/r/commandline/comments/19garq/a_sampling_of_coreutils_120/ A sampling of coreutils] [http://www.reddit.com/r/commandline/comments/19ge6v/a_sampling_of_coreutils_2040/ , part 2] [http://www.reddit.com/r/commandline/comments/19j1w3/a_sampling_of_coreutils_4060/ , part 3] - An overview of commands in coreutils

This article deals with so-called "core" utilities on a GNU/Linux system, such as less, ls, and grep. The scope of this article includes - but is not limited to - those utilities included with the GNU coreutils package. What follows are various tips and tricks and other helpful information related to these utilities. If sections grow too detailed, please split into separate articles.

Contents

cat

cat (catenate) is a standard Unix utility that concatenates and lists files.

As cat is not a shell built-in, in many occasions you may find more convenient to use a redirection, for example in scripts, or if you care a lot about performance. In fact $ < file does the same of $ cat file.

To append multiple lines to a file a construct like this one is used:

cat << EOF >> path/filefirst line
...
last line
EOF

If you need to cat file lines in reverse order, there is a utility called tac (cat reversed).

grep

grep (from ed's g/re/p, global / regular expression / print) is a command line text search utility originally written for Unix. The grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.

The environment variable GREP_COLORS may be used to specify different colors than the defaults.

ifconfig

ifconfig (interface configuration) is a system administration utility in Unix-like operating systems to configure, control, and query TCP/IP network interface parameters from a command line interface (CLI) or in system configuration scripts.

less

less is a terminal pager program used to view the contents of a text file one screen at a time. Whilst similar to other pages such as more and pg, less offers a more advanced interface and complete feature-set.[3]

lesspipe also grants less the ability of interfacing with files other than archives, serving as an alternative for the specific command associated for that file-type (such as viewing HTML via html2text).

Re-login after installing lesspipe in order to activate it, or source /etc/profile.d/lesspipe.sh.

Vim as alternative pager

Vim (visual editor improved) has a script to view the content of text files, compressed files, binaries, directories. Add the following line to your shell configuration file:

~/.bashrc

alias less='/usr/share/vim/vim73/macros/less.sh'

ls

ls (list) is a command to list files in Unix and Unix-like operating systems.

Colored output can be enabled with a simple alias. File ~/.bashrc should already have the following entry copied from /etc/skel/.bashrc:

alias ls='ls --color=auto'

The next step will further enhance the colored ls output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to your shell configuration file:

~/.bashrc

eval $(dircolors -b)

man

man (manual page) is a form of online software documentation usually found on a Unix or Unix-like operating system. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts. See Man Pages.

mkdir

mkdir (make directory) is a command to create directories. To create a directory and its whole hierarchy, -p switch is used, if not a error is printed. As users are supposed to know what they want, -p switch may be used as a default.

alias mkdir='mkdir -p -v'

The -v switch make it verbose.

Note: If you want just a temporary directory a better alternative may be mktemp (make termporary): $ mktemp -p.

mv

mv (move) is a command to move and rename files and directories. It could be very dangerous so it is prudent to limit its scope:

alias mv=' timeout 8 mv -iv'

This alias suspends mv after eight seconds, asks confirmation to delete three or more files, lists the operations in progress and does not store itself in the shell history file if the shell is configured to ignore space starting commands.

rm

rm (remove) is a command to delete files and directories. It could be very dangerous so it is prudent to limit its scope:

alias rm=' timeout 3 rm -Iv --one-file-system'

This alias suspends rm after three seconds, asks confirmation to delete three or more files, lists the operations in progress, does not involve more than one file systems and does not store itself in the shell history file if the shell is configured to ignore space starting commands. Substitute -I with -i if you prefer to confirm even for one file.

sed

sed (stream editor) is a Unix utility that parses and transforms text.