Manage Linux files and directories with Dired and Emacs

Daily Newsletters

Sign up to ZDNet UK's daily newsletter.

ANALYSIS
While working with Linux, I found myself constantly switching back and forth between my text editor (to create and edit files) and a terminal window (to use the shell commands to manage those files). Then I learned that the Emacs text editor has a built-in directory editor called Dired that lets you manage your files and directories without ever having to leave the Emacs window. Dired allows you to select a file in a directory list and perform an operation on it by entering in a command (e.g., entering c for copy or e to open a file for editing.) Whether you're looking to learn how to use Linux yourself or needing to train your end users, this short lesson will show you how Dired directory editor works with the Emacs text editor. Working with Dired
First, create a text file called about_emacs by entering the following at a command prompt:emacs about_emacs After pressing [Enter], you'll see the Emacs text editor screen, as shown in Figure A. Note that the screen is divided into three main areas, the Edit area where text is typed, the Mode line where status information is displayed, and the Echo area, where commands are entered and prompts and messages from Emacs are displayed.
Figure A
Next, type a short sentence in the Edit area. To save the changes to the file, press [Ctrl]X, then [Ctrl]W. Enter the name of the directory you want to save your file to at the prompt in the Echo area like this:Write file: "/home/Mary/Emacsdemo
Pressing [Enter] saves the file about_emacs to the directory Emacsdemo. Now let's switch from the text editor to Dired. Press [Ctrl]X, then D. At the prompt in the Echo area, type the name of the directory you want to display as in the following:Dired (directory): /home/Mary/Emacsdemo
After pressing [Enter], the Edit area will display the directory listing, as shown in Figure B.
Figure B
Returning to Edit mode from Dired
Next, you'll need to open about_emacs from the directory list. Position the cursor on the last line within about_emacs by either pressing the N (new line) key, press the down arrow to get to the line, or simply click on the line with your mouse. Once the cursor is positioned, press the E key to open the file and return to edit mode. Now add the date to the file, as shown in Figure C.
Figure C
To save the changes, press [Ctrl]X, then [Ctrl]S. Returning to Dired
To return to Dired, press [Ctrl]X, then press the D key. The directory list is displayed as shown in Figure D.
Figure D
As instructed in the Echo area, typing G will update the listing (Figure E).
Figure E
Note that by default Dired sorts the files in alphabetical order by name. You can also have them listed by date. Pressing the S key will sort the list by date and time, starting with the most recent file. Pressing S again will resort the list by name. Creating a new directory
To create a new directory with Dired, press the Plus symbol key (+). You will then be prompted for the directory name in the Echo area. For this example, call the new directory backup. After pressing [Enter] the backup directory will be added to the listing, as shown in Figure F.
Figure F
Copying files
To use Dired to copy the file about_emacs to the backup directory, first position the cursor anywhere within the filename. Then, press [Shift]C. A prompt will appear in the Echo area asking for the name of the directory where you want the file to be copied, which for this example is the backup directory (Figure G).
Figure G
Renaming a file
To rename a file, first position the cursor over the filename (in this example, about_emacs), and then press [Shift]R. A prompt will appear in the Echo area, asking for the new name; for this example, you would enter about_emacs_v1, as shown below:Rename about_emacs to :/home/Mary/Emacsdemo/backup/about_emacs_vl
After pressing [Enter], the new name will appear in the listing (Figure I).
Figure I
Changing group permissions
To change a file's group permissions, move the cursor to the filename, in this case, about_emacs_v1, and press [Shift]G. You'll receive a prompt in the Echo area asking for the new group. Enter the name of that group at the prompt, as shown in Figure J.
Figure J
After pressing [Enter], the new group will be added to the list as shown in Figure K.
Figure K
Viewing and printing a file
Dired also lets you view a file without going back into Edit mode. To do so, position the cursor over the name of the file you want to view, and press V to display the contents of the file in read-only mode. To return to the directory listing, press Q (quit). You can also print a file by positioning the cursor over the name of the file you want to print, and pressing [Shift]P. Deleting a file
You can also use Dired to delete files from a directory list. First, flag each file to be deleted by positioning the cursor over the name of the file you want deleted and pressing D. (If you mark a file for deletion by mistake, pressing U will clear the D.) Three files flagged for deletion are shown in Figure L.
Figure L
Pressing X will delete the files flagged with D. Note that Dired will display a message in the Echo area asking whether you want to delete the flagged files before actually deleting them. Executing shell commands
Dired lets you execute shell commands on any file or directory within a directory list by positioning the cursor on the file or directory (in this example, backup), and then press [Shift]1. The Echo area will then prompt for the command you want to use; in this example, ls is used to list the contents of the backup directory to the file backuplist as shown in Figure M.
Figure M
Pressing [Enter] executes the ls command. Quitting Dired
To get back to Edit mode from Dired, press Q. With a little practice, I've found working with Dired and the Emacs text editor a lot quicker and easier than having to work with two or more terminal windows to create, edit, and manage files and directories.
Have your say instantly in the Tech Update forum. Find out what's where in the new Tech Update with our Guided Tour. Let the editors know what you think in the Mailroom.

Post your comment

In order to post a comment you need to be registered and logged in.

You can also log in with Facebook. Log in or create your ZDNet UK account below

  • Login

Will not be displayed with your comment

By signing up for this service, you indicate that you agree to our Terms and Conditions and have read and understood our Privacy Policy. Questions about membership? Find the answers in the Community FAQ

Get ZDNet UK's daily newsletter

Enter your email address to sign up

ZDNet UK Live

BrownieBoy

> I'm told it's somewhat annoying when people have their Macs stolen > and Apple stores treat the thief as the owner, but there you go. Ouch,...

13 hours ago by BrownieBoy on AMD Ultrathins to challenge Intel Ultrabooks
Moley

@kevinmchapman. OK, I acknowledge that 'most' was a gratuitous throwaway comment as an afterthought and too presumptuous. As to proof, as you...

17 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
Jack Schofield

@BrownieBoy > Works really well for thieves.... >> Nice attempt to deflect the argument by tossing in a point that's totally >> irrelevant, even...

18 hours ago by Jack Schofield on AMD Ultrathins to challenge Intel Ultrabooks
raskolnikof

fantastic that the so called piracy bills have been withdrawn. however, these anti-democracy supporters are still in the shadows so lets be alert...

19 hours ago by raskolnikof on SOPA, Protect IP support wavers in face of online protest
Tony Douglas

Please God no; teach them anything you like - thinking rationally, the uses and misuses of data, what data is and what it's not - but leave the...

21 hours ago by Tony Douglas via Facebook on Kids are the future. Teach ’em to code.
BrownieBoy

@Jack, > Works really well for thieves.... Nice attempt to deflect the argument by tossing in a point that's totally irrelevant, even it were...

1 day ago by BrownieBoy on AMD Ultrathins to challenge Intel Ultrabooks
bootlegger

Make that 13 people now - I got refused today at Manchester airport. I thought I was up to date on this legislation - I knew of the EU ruling from...

2 days ago by bootlegger on UK airport body scans will not be opt out
tinycg

Don't forget to check out apps like GoodReader or SlideShark either, they're indispensible for people on the go in presentation situations. Best...

2 days ago by tinycg on Four top iPad apps for people on the move
TerryRK

Well it seems there is something a number of us agree on. Why is the Ubuntu Unity launcher so ugly? I thought perhaps it was something to do with...

2 days ago by TerryRK on A tale of two distros: Ubuntu and Linux Mint
Freebies202

Duplicate comments are not made intentionally. Its very good to know that now you are keeping check on this problem because sometimes a commenter...

2 days ago by Freebies202 on Microsoft fixes blog comments, speeds up blogs with open source
kevinmchapman

"the very significant number of users" and "many (most) of us" - you have no evidence for these statements. It is a fact that most users are saying...

3 days ago by kevinmchapman on A tale of two distros: Ubuntu and Linux Mint
Marg Menzies Harrison

Another grammar faux pas is the improper use of "you". When sitting down down in a restaurant, for example, I get cringe when the waitress...

3 days ago by Marg Menzies Harrison via Facebook on 10 flagrant grammar mistakes that make you look stupid
zdnetukuser

And NOW, folks, for Canonical's next trick... Kubuntu is late. Here's a pencil. Draw your own conclusions. cf.:...

3 days ago by zdnetukuser on Linux Minterface
Moley

@kevinmchapman. The discussion here reflects the very significant number of users who really do like the traditional menu system and who wish to...

3 days ago by Moley on A tale of two distros: Ubuntu and Linux Mint
kevinmchapman

Er, no... It is an efficient means of finding the application/file/setting you need in one place. The icons are a simply a fallback for when you...

3 days ago by kevinmchapman on A tale of two distros: Ubuntu and Linux Mint
TerryRK

Isn't the provision of a text based search an admission by the developers that the mass of icons approach does not work? I don't need to use a...

3 days ago by TerryRK on A tale of two distros: Ubuntu and Linux Mint
kevinmchapman

"Unity and GNOME 3 both abandon the old text-based cascading menus in favour of a graphical icon-driven system." Point truly missed. Both use a...

3 days ago by kevinmchapman on A tale of two distros: Ubuntu and Linux Mint
TerryRK

whs001 - Thank you, I'm glad you liked the article. I absolutely agree with you on your first point. I should perhaps have made it clearer that...

3 days ago by TerryRK on A tale of two distros: Ubuntu and Linux Mint
Dennis Nilsson

If we allow corporate interest to dictate the way our government circumvents due process against foreign entities then we should accept the same...

3 days ago by Dennis Nilsson via Facebook on ACTA stumbles in Germany
GHar123

I totally dislike pirating of works, I fear that artists will be deterred from creating works if they think that they are going to get ripped off....

3 days ago by GHar123 on ACTA stumbles in Germany