quickstart: File Handling in Linux CLI

Here are some commands I use to list and search files in Linux using a command line interface.

Files:

  • "locate <filename>" - Locate works with its own file name database. The database indeed requires updating to reflect change (done by command "updatedb"). Locate works with file names, it doesn't look inside files.
  • "find / -name <filename> -type f" - Gives all files ('-type f') with the name '<filename>', starts finding at root ('/') and continues searching recursively through subdirectories.
  • "find / -name <filename> -type d" - Gives all directories ('-type d') with the name '<filename>'etc.etc. ('-executable' Gives all executable files...)
  • "ls" - Lists directory contents. Add "ls -l" for long listing format, "ls -C" to list entries by columns, "ls -t" to sort output by time, and "ls -s" to sort by size.
  • "stat <filename>" - Lists the last file access, by which user and time of last file modification.
  • "cd <directoryname>" - Go to directory. "cd .." Go one directory up. "cd /" Go to root directory.
  • "cp -r <source> <destination>" - Copy a file or directory to a new location. Including eventual sub directories and files ('-r' i.e. recursively).
  • "mv -r <source> <destination>" - Move a file or directory to a new location. Including eventual sub directories and files ('-r' i.e. recursively). Btw, you can also use mv to rename a file: "mv 'oldname' 'newname'".
  • "rm -r <filename>" - Remove files or directories including sub directories and files ('-r' i.e. recursively).

Files' contents:

  • "cat" - Prints files' content to standard output (usually, that would be your screen).

You could, e.g., type "cat /etc/netconfig", which would print the contents of the file netconfig (which is located in the directory 'etc', which is located at the root '/').

Piping output:

  • "|" - Pipe. Pipe enables you to push the output of, e.g., "cat" to somewhere else. This would mean that you are sending the text content of netconfig somewhere else. (e.g., to "grep").

Filtering output:

  • "grep <keyword>" - Filters input on a search string of your choice.

Say you would type "cat /etc/netconfig | grep tcp". Brake down as follows:

  1. "cat /etc/netconfig" outputs the contents inside the file netconfig.
  2. "|" Pipe, would take that output and pipe it to:
  3. "grep tcp" Grep filters that output, for the keyword 'tcp'.

By result, only the text lines inside the file netconfig containing 'tcp' will be printed to the screen (as opposed to the entire content of the file 'netconfig' (which would be the result of only using cat, and not piping that output to our friend grep).

To view or add a comment, sign in

More articles by Roger van Oordt

Insights from the community

Others also viewed

Explore topics