KC7MM Wiki

Amateur Radio with KC7MM

User Tools

Site Tools


linuxusernet:inside_linux:files

A Look Inside Linux series

A series of short topics on how Linux works and how to make it work the way you want it to, which I present during the weekly sessions of the Linux User Net. The target audience is Hams who are new to Linux and want to know more about it, as well as experienced Linux users who can learn more about their chosen operating system. These are my notes for the presentations. (Russ, KC7MM)

16. Working with files in Linux

Having covered the Linux file system, we'll now turn our gaze to the files themselves. Specifically, we'll look at files that contain data – although some of what we'll discuss also applies to user-owned directories. (Remember, “everything is a file”.)

We'll concentrate on practical aspects of working with files in the Linux file system. We'll see how to work with them at the Bash prompt, as well as with a graphical file manager. If you have a Linux box available, you can use it to follow along.

File names

  1. Legal characters:
    • Alphanumeric characters.
    • Underscore, dash, dot, space.
    • No restrictions on character order.
    • Advise against using spaces – they cause difficulties at the command line.
  2. Format:
    • File name, with extension after a final dot. There can be multiple dots.
    • The extension is a convention, and has no significance within the file system itself. It may be used by programs to try to identify the file type (more on this later).
    • Dashes in filenames can be a problem in Windows. You might want to avoid using them with files that you expect to be shared with a Windows machine.
  3. Hidden files:
    • Filename begins with a dot.
    • By default, don't show up in file listings.
      • ls -a to view at command line.
      • Ctl-H toggles hidden file views in GUI file browser and file save/open dialog boxes.
      • There are a lot of these in a user's home directory.

File types

MIME types

In Linux, the type of data contained in a file is identified by its MIME type.

  1. MIME is an acronym for Multipurpose Internet Mail Extensions.
  2. The MIME specification format is <class>/<format>, where:
    • <class> is a general class of data, such as text, image, application.
    • <format> is a specific data format.
    • Examples: text/html, text/plain, image/x-icon, text/x-script.python
  3. The MIME type may have one or more recommended file extensions that can be used to quickly identify a file type. For example, .py for Python programs or .ico for icons.
  4. References

Identifying types

Identifying the type of file varies with the tool you use.

  1. In Bash: the file command.
    • file .bashrc identifies its content as “ASCII text”.
    • Adding the “i” argument returns the MIME type: file .bashrc -i identifies its content as text/plain; charset=us-ascii.
    • Running file on file that contains a compiled program:
      • file /usr/bin/aplay returns extensive information on the program, including liked libraries.
      • file /usr/bin/aplay -i returns the MIME type application/x-sharedlib; charset=binary.
  2. In a GUI file browser:
    • MIME type is shown in a Properties dialog.
    • The extension also can be used – in addition to the MIME type – to identify an application that can work with the file. For example, a text editor for Python script that has a .py extension.

Viewing file content

Taking a look at the contents of a file is most easily done at the command line. Two commands are most useful for this:

  • cat command prints the contents of a file in a terminal.
    • Example: cat .bashrc
    • Works best with small files that don't scroll offscreen.
  • less command lets you view a file interactively, with scrolling and search capabilities.
    • Example: less .bashrc
    • Scroll with arrow, pageup, pagedown keys, plus spacebar.
    • Search by pressing the slash key (/), then type in the search string (a regular expression). Matches will be highlighted. You can move through them with the N key for the next match, and the P key for the previous match.
    • Exit by pressing the Q key.
  • Archive files, such as the .tar.gz, .zip types, present a special case. There are command-line utilities to work with them, but I find it far easier to handle them in a GUI file browser that employs an archive manager.

Finding files

There are many thousands of files on your Linux box. Finding a particular file can be a challenge. A Linux desktop system provides file search capabilities at the command line and in a graphical file manager.

Command line

I tend to make command-line searches, using two utilities: ls and find.

ls command

The ls command is good for listing files that you can scan to find the one you want. It works best when looking in the current directory, or some other single directory. Let's look at how it works.

  • ls returns a single-line list of files and directories in the current directory.
  • ls <pattern> returns a list of files in the current directory that match the pattern.
    • Example: ls *.pdf lists PDF documents.
  • ls -R does a recursive search, returning all files in the current directory and in all subdirectories. It does not allow you to specify a pattern to narrow down the list.

find command

The find command is useful for finding particular types of files in places other than the current directory.

Let's say you want to locate icon files that are installed on your system. You know that that they're likely to be somewhere under the /usr directory, since that's where applications are installed. You also know that icon files generally have an extension of .ico.

You can use the find command to locate them:

find /usr -iname '*.ico'

Let's look at what's going on here. You're running the find command, telling it to start its search in the /usr directory, and display all files with a name the ends in .ico. Very neat. For more on how to use this command, look here.

GUI file manager

Every file manager I've used has had some search capability. There are many file managers, each with its own interface, so I won't try to cover them here. Suffice to say that the app on your system has the ability to let you find files, if you learn to use it.

One reason I prefer the command line for this task is that it works the same in all Linux systems. I have to climb the learning curve only once, and then I can use it anywhere.

Managing files

Just a quick look at what's available on Linux systems for managing files.

  • In Bash:
    • mv to rename
    • mv to move to another directory
    • cp to copy
    • rm to remove. Cannot be undone
    • Tab completion for filenames
  • File manager:
    • Context menu to rename
    • Ctl-X to cut
    • Ctl-C to copy
    • Ctl-P to paste
    • Delete for reversible removal, Shift-Delete for permanent removal
    • Mouse for file selection and moving

Wrap-up

Linux gives you all the tools you need to work with your file system. You can use the command line, a graphical file manager – or both. You get to choose the tool that works best for you.

Return to series index

linuxusernet/inside_linux/files.txt · Last modified: 2022/04/26 15:21 by KC7MM