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

Tutorial: Introduction to Linux files

Up
vote
Down

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
tardir.0.log
$
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:

 home/tfr/
home/tfr/doc/
home/tfr/doc/1-WAY
home/tfr/doc/0
home/tfr/doc/ADMIN
[ . . . ]
home/tfr/doc/BAT
home/tfr/doc/BATT
:
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
tardir.0.log
$ mv tardir.0.log tar.log
$ ls
tar.log
$
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
home/tfr/timeout.html
home/tfr/hello.html

$
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'
/bin/echo
/etc/xinetd.d/echo

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

$

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:8904 | -Ray, April 2, 2005
More Linux articles...

Tallahassee Abstract Art

linux headlines

Wifislax 4.6 review

Fedora 19 review Schrodingers Cat

4 third-party repositories for Fedora 19

StartOS 6 GNOME 3 and KDE preview

BackTrack 5 or Kali Linux 1.0

Fedora 19 preview

Tutorial: Build a CentOS 6.4 x86_64 Server

Tutorial: Dual-boot fully encrypted Fedora 18 and Windows 7

Chakra 2013.02 Benz review

Sabayon 11 GNOME, KDE, MATE, and Xfce preview

Tutorial: Install Debian 7 (testing) with debootstrap from a Grml live Linux

How to customize Linux Mint 14 Cinnamon

Linux Mint 14 MATE and Cinnamon preview

Tutorial: Replace Windows with Kubuntu 12.10

Beginners Guide To Ubuntu One Personal Cloud

Linux Tutorial: View Page Faults Statistics

Tutorial: Replace Windows with OpenSUSE 12.2

Dual-boot Windows 7 and Ubuntu 12.04 on a PC with UEFI board, SSD and HDD

Book Review: Ubuntu Made Easy

Chakra Linux 2012.08 preview

BlankOn 8 preview

ls* commands more useful than you know

Tutorial: Replace Windows with Linux Mint 13 KDE

 

Firefox sidebar

Site map

Site info

News feed

Features

Login
(to post)

Search

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