Skip main navigation

Hints, Tips and Redirects

Linux shortcuts and redirects screencast
7.2
Hello. In this video, I’ll personally show you three or four handy tips to make your life using the command line much easier and then move on to showing you how to redirect the output of commands into files, also show you how to input data from files into commands, and also how to combine commands together. The first thing I’ll do is open my Terminal and then maximise it. The first thing I’ll do is type “ls” to remind me of the contents of my home directory. I want to cd into the course_data directory. I could type this out in full. However, if I type “cd” and then type “cou”, and then hit the Tab key, it completes the command for me.
51.4
And then press Enter and I’m in my course_data directory. This is called tab complete. It’s one of the most useful things you learn about the command line and minimises the amount of typing you’ll do and it really minimises the amount of typos you’ll introduce. As well as completing the names of files and directories, tab complete can be used with commands. For example, I’ll use the Linux command which. This shows you where the location of a binary file is that’s running a programme. I’ll start by typing “whi” and hit Tab. There must be more than one option for this, so I hit Tab again. This shows me that I have, which, while and whiptail as three programmes installed.
94
I’ll type the “c” and then type the Tab and then type “ls” and that shows me the location of ls. One other handy thing is that you can use the up and down arrows to scroll through your old commands. For example, I ran ls earlier. I could obviously type “ls” now, but for demonstration purposes, I can scroll back through using the up arrow, do “ls”, and now run ls on the current directory. Linux also has a command called history. This allows you to view all of the commands you’ve run recently. If I type “his”, then tab complete the history, it shows everything I’ve run recently, numbered. This can be used to cut and paste old commands if you need to.
137.2
Another useful thing is that Linux has keyboard shortcuts, which allow you to go the start and end of the current line. To go the start, use Control-A. To go to the end, use Control-E. For instance, I start typing “which ls” then notice that I missed that I missed the “ih”. Rather than using the arrow to scroll back, I can just press Control-A and then two arrow presses and then the “i” and then I can press Control-E to go to the end of the line and run “which ls.” Next, we’ll see how to wildcards to group things together. The wildcard is represented by the asterisk character.
176.2
Firstly, if I run else in this directory– let’s say there are several files ending in “.txt.” If I only want to list these, I can type “ls” and then “*.txt.”
191.2
And now only the “.txt.” files are listed. So far, you’ve only seen how to output things to the screen. But now we’re going to show you how to output things to file. If I run “ls -l” here, that outputs to the terminal. If I want to write this to file, I can run “ls -l”, then use the greater than sign, and then type “ls.out.”
220.2
If I then run “ls -lrt”, you’ll see that we now have a file “ls.out.” If I use the command less to view this, it shows you the output of ls -l in the file. We can use a double greater than sign to append things to files. For example, I’ll use the command echo, which writes any text you give it to the desired output, to append something to ls.out. I’ll type “echo” double quotes “Hello world” and then double greater than and “ls.out.”
271.1
If I then scroll back to run less ls.out, we’ll see that “Hello world” is appended to the end of the file. And quit that. To overwrite a file, you can simply use the single greater than sign. So if I want more, I’ll type “ls -l” into ls.out and then run less ls.out. You’ll see that we no longer have “Hello world” appended to the bottom. While the greater than sign is used to output data into a file, the less than sign can be used to input data from a file. Through a demonstration of this, I’ll use the file “theme.txt” and I’ll use the Unix command “sort”, which run at its most basic will sort the data alphabetically.
321.9
I’ll start by showing you “theme.txt.”
329.3
You can see that there are three lines, each containing a book title and a category that it’s entered into.
337.9
If I run “sorts” and then provide the less than and then “theme.txt”, it outputs them alphabetically, and we’ve taken the input using the less than sign. The final thing to show you is how to direct the results of one command into another. This is done using the pipe character, this one. To do this, I’ll use sort and I will use “Diamonds_fix.txt” as the input and that I’ll direct this into less, so we don’t just sort things into the screen and because “Diamonds_fix.txt” is a long file and we’re just going to the end. So when I do this, you’ll see that this is sorted alphabetically and that it’s gone into less, so I can scroll through one screen at a time.
395
Many thanks for watching this video. Please leave all questions and comments in the comments section below.

Watch this screencast by Martin

Martin gives some tips on the Linux shortcuts and redirects.

Hope you find this video useful in finding your way around in a Linux environment.

If you have any comments please leave them in the comments section below, thank you.

This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education