(Show all Tutorial articles . . .
Headlines | Linux | Apps | Coding | BSD | Admin | News
Information for Linux System Administration 

Tutorial: Introduction to Linux files


This newbie-level Linux tutorial is an introduction to handling files from the Linux command line. It will cover finding files, determining their type, renaming, copying, examining their attributes, reading their contents, and, in the case of binary files, how to get clues to learn something more about them. Further reading will be suggested for editing files since that topic is beyond the scope of this article.

Getting Started
The reader of this tutorial is expected to have access to a Linux system and to perform the example commands as we progress through the tutorial. Once logged in to your Linux system, open a terminal session. Under Red Hat Linux, terminal is found in the 'system tools' section of the menu. (Your system may, alternatively, use a terminal program called 'konsole', 'xterm', or 'shell'. Look around your system for a menu with 'tools' or 'utilities' in the name if necessary.)

ls: Listing files
Let's start with the ls command. ls is an abbreviation for list files. Type ls now, then press the 'enter' key to see the names of the files in your current directory. The results from my 'tmp' directory are listed in bold below:

 $ ls /tmp
Note that I said 'your current directory'. To get the a listing of files in another directory, enter ls [dir] where [dir] is the name of the directory you wish to look at. For example, to see the file names in your top level directory, '/', type the following:
 $ ls /
bin dev home mnt
proc sbin tmp var boot etc
initrd lib opt root sys usr

For more information on the files, use one or more of the ls command line switches. Here I use ls with the -l switch for a 'long' listing:
 $ ls -l
total 14
-rw-r--r-- 1 root root 9649
Mar 28 02:47 tardir.0.log

Note that with the -l switch we get the file permissions, the inode links, the owner and group names, the file size in bytes, and the timestamp of the file as well in addition to the name. The ls command has many more options. Type man ls for a full list of options.

file: What is this file?
Linux also provides a handy command to help determine what type of files you are dealing with:

 $ file tardir.0.log
tardir.0.log: ASCII text
The Linux (and Unix) file command knows about, and can detect, many different file types. In our example, file tells us that tardir.0.log is a simple ASCII text file.

less: Paging through a file
Now, to actually look at the contents of a text file, we have many options. The most common is the more command and a more elaborate, newer command is less. I like less because it lets you use the arrow keys for scrolling and the pgup/pgdn keys for paging through the file. The following is a condensed page from the command less tardir.0.log:

[ . . . ]
From the ':' prompt we can page or scroll forward or backward. We can also type /star to search for the next occurrance of the string 'star'. Enter man more or man less for more information on the more or less commands, respectively.

mv: Renaming a file
Now, suppose we want to rename a file. Under Linux (and Unix) we 'move' it with the mv command as follows:

 $ ls
$ mv tardir.0.log tar.log
$ ls
Note that the mv command only produces output when there is an error. In this case, we encountered no error so mv quietly performed its work.

cp: Copying files
To make an actual copy of a file, we use the cp command. For example, to make a backup copy of tar.log named tar.log.2, we enter the following:

 $ cp tar.log tar.log.2
$ ls
tar.log tar.log.2
Again, we get no output to the screen when the cp command is used without error. We had to use the ls command to see the result of the command. Enter man cp for more details of the cp command.

strings: Looking for text in a binary file
Now, to actually look inside an unknown binary file for text strings there is a command called, appropriately enough, strings. For example, if we run the strings command on the 'echo' program, we get, in part, the following:

 $ strings /bin/echo
Copyright (C) 2004 Free
Software Foundation, Inc.
Written by %s, %s, %s,
%s, %s, %s, %s,
%s, %s, and others.

Type man strings for more information.

grep: Finding particular strings in a file
To look for a particular text string in a file, we use the grep command:

 $ grep html tar.log

And, of course, man grep will retrieve additional instructions for the grep command.

find: Finding files by name
To find all files with a particular name on your system, use the find command. For example, to find files named 'echo', enter the following:

 $ find / -name 'echo'

Further, to find all files in the /var filesystem with the string 'echo' in their names, enter this:
 $ find /var -name '*echo*' 


More information...
To get started editing text files try this tiny vi tutorial. After going through the quick tutorial, you can click the contents button and reach an advanced vi tutorial as well as other vi information.

For information on moving around in a Linux filesystem try this Introduction to Linux in ten commands. That article also provides additional examples on some of the commands covered here.

mail this link | score:9453 | -Ray, April 2, 2005
More Linux articles...

Abstract Art Prints for Sale

linux headlines

10 basic examples of linux netstat command

Parsix 5.0 review

Tahoe Least-Authority File System for secure, distributed data storage

Ubuntu Edge: Is there life after an unsuccessful crowd-funding campaign?

Elementary OS 0.2 Luna review

Precise Puppy 5.7.1 review: a small and swift linux distro

Wifislax 4.6 review

Reviewing Kali Linux - the distro for security geeks

Upgrade Fedora 18 to 19 With FedUp

Basic nginx Configuration

Korora 19 review

ROSA Desktop R1 GNOME preview

Fedora 19 review Schrodingers Cat


Firefox sidebar

Site map

Site info

News feed


(to post)


Articles are owned by their authors.   © 2000-2012 Ray Yeargin