Linux tutorial, part 4
Looking at file contents with the "more" and "cat" commands
If I want to be sure that I have the right file I can also look at it with the more
command, like this:
If I want to be sure that I have the right file I can also look at it with the more
command, like this:
Now, if I'm really cool, I don't actually type out that whole remove command, do I? As a practical matter I usually just type in something like this:
rm de
and then hit the [Tab] key, and if "delete.me" is the only file in the current directory beginning with the characters "de" the Unix system expands my command line to look like this:
rm delete.me
Pretty cool, eh? That part is called "command-line expansion", and it makes life very easy.
Linux ls command FAQ: Can you share some examples of the Unix/Linux ls
command?
The Linux ls
command is used to list files and directories. While it has many options, I thought I'd list the most common ls command uses I'm aware of.
The ls
command options I use most of the time are -a
("show all") and -l
("long listing"). Put together, like this:
The Linux cd
command is used to navigate around the Linux filesystem. In this post I'll show the most common uses of the cd
command.
To move to another directory on the filesystem just use the Linux cd
command to move to the desired directory. For instance, this command:
cd /tmp
moves you to the /tmp
directory, and this command:
cd /foo/bar
would move you to a directory named /foo/bar
, assuming that directory existed.
The Linux grep
command is used to search for text. The name "grep" means something like "general regular expression parser", and if you look at the grep man page it says "print lines matching a pattern". I always tell people that if they don't like the name "grep" they can think of it as "search" instead.
In "grep tutorial", we'll share a number of grep example commands. Let's get started.
The Linux cp
command lets you copy one or more files or directories. To be clear, the cp
command makes a duplicate copy of your information, whereas the mv
command moves your information from one location to another.
Let's take a look at some Linux cp command examples.
Using a Linux or Unix system, to copy a file named "foo" to a new file named "bar" just type:
The Linux mv command lets you move one or more files or directories. Since it's very similar to the cp
command, I'll move through this post quickly.
To rename a file currently named "foo" to a new file named "bar" just type:
mv foo bar
Although it's called the Linux mv command, it's commonly used to rename files.
To move a file named "foo" to the /tmp
directory type:
The Linux cat
command means "concatenate and print files". Usually all I use it for is to display a file's contents, like this command that displays the contents of a file named "lighten-images.sh":
As its name implies, the Linux mkdir
("make directory") command lets you create new directories.
Creating a new directory in your current directory is very simple. In our first mkdir example, we'll show how to create a new directory named "dir1":
mkdir dir1
If you want to create several directories at one time you can use a mkdir command like this:
The Linux locate
command lets you easily find files in the filesystem. It works by maintaining a system-wide database of "all files which are publicly accessible". The database itself is updated periodically by a background process. Because of this approach it returns results much faster than the find
command, which only looks for files when you tell it to. Depending on your system, the locate
command may need to be configured initially, or it may be pre-configured to work out of the box.
Linux/Unix more command FAQ: Can you share some Linux more command examples?
The Unix/Linux more command lets you scroll through large files, or large amounts of output from other commands.
To scroll through the contents of a large file named "large_file" you can use the Unix more command like this:
more large_file
As a quick aside, I see a lot of people use the Linux cat command and more commands this way:
The Linux tar
command is used to created and extract archives. An archive is one file that contains one or (usually) many other files. The name "tar" itself comes from the phrase "tape archive", but that's just an old name. I mostly just create archives and then send them over the wire these days.
To create an archive of all files in your current directory, and all subdirectories, use this tar command:
Here's one of my favorite Linux shell scripts. I use it to change the extensions of a bunch of files all at once, i.e., to batch rename multiple files with one command.
For instance, I just had a situation where I got a bunch of files with an extension of .PNG
, using capital letters like that. That's no big deal if you have a few files to deal with, but what if you have 200 or more? That's where this script, named mmv
, comes in.
Using my shell script is really simple, like this:
Here's a Unix shell script that converts all "*.png" files in the current directory to lower-case names. In my case I had files named "Slide1.png", etc., and I wanted them to be named "slide1.png", and this script did the trick.
Summary: How to reverse the meaning of a Linux find command.
I spent last night doing a bunch of work on my source code warehouse. At the end of the night I needed to do a search for all files in many subdirectories whose filenames did not end with "*.java". It's easy enough to find filenames that do end with "*.java", using the find command like this:
vi/vim FAQ: How do I use the vim completion (word completion) feature?
When using the vi/vim editor I'm surprised how many people don't use its built-in vim completion (vim auto complete or vim code complete) feature.