What command enables you to find all empty files in your source directory?

Out of the hundreds of possible Linux commands you could learn, here are the ten basic ones you really need to know.

Posted: August 22, 2019 |%t min read| by Ken Hess (Sudoer alumni, Red Hat)

Image

What command enables you to find all empty files in your source directory?

Whether you're a Windows system administrator looking to expand your skills into Linux, a fresh convert to Linux, or someone who's looking to find a job in IT, this introduction to some common Linux commands is for you. These 10 commands are the ones that Linux system administrators use frequently—in fact, they use at least eight of them on a daily basis. And no matter how long you use Linux, you'll always use these commands. Some of you MS-DOS users will recognize a few of these and, not surprisingly, they have the same function in both operating systems.

ls lists directory contents

More Linux resources

  • Advanced Linux commands cheat sheet
  • Download RHEL 9 at no charge through the Red Hat Developer program
  • A guide to installing applications on Linux
  • Linux system administration skills assessment

The list (ls) command is equivalent to the DOS DIR command, in that it lists files and directories. If you simply type ls at a prompt ($), you'll see all non-hidden files in your current directory, which is your home directory when you first log into a Linux system. The ls command won't show you much in your home directory on a new system, so let's explore a directory that contains a lot of files and directories: /etc. The /etc (et-see) directory is where a Linux system's configuration files live.

$ ls /etc

A large number of files (over 200) appear on your screen. You've successfully listed the contents of the /etc directory, but you can actually list files in several different ways. Above, I mentioned non-hidden files. In your home directory, where you are now, you probably have hidden files. Hidden files in Linux begin with a period (ls0). For example, you likely have a ls1 file there. To see it, use the following ls command.

ls3

You now see several files beginning with a period. The ls4 switch—or option, as it's called—shows you all files, even hidden ones.

ls5 displays manual pages

lsls7 and a screen-full of information appears before you. 

It's easy to navigate man pages. Use the Enter key to advance one line at a time, the 'ls8' key to go back, the Space bar to advance a full-screen page, and the 'ls9' key to exit the man page. As an example, look at the man page for the ls command.

DIR1

DIR2 concatenates files

The DIR2 command is important as a basic command because it serves two very important functions: concatenating (merging) files (as the name suggests) and printing the contents of a file to the screen. Printing the contents of files is by far the more frequent use of this command. If you want to see a file's contents, use the following format:

DIR4

For example, you might type the following to display the contents of the system's DIR5 file on the screen:

DIR6

To use DIR2 for its file concatenation powers, the general form of the command is:

DIR8

For example, to redirect the contents of DIR9 and ls0 into the ls1 file:

ls2

You can concatenate as many files as you want into a single file using DIR2.

[ Free download: Advanced Linux commands cheat sheet. ]

ls4 changes file timestamps

The ls4 command is another one that serves a dual purpose. Its designated purpose is to update the timestamps on files. If you list the contents of a directory in long format with:

ls6

The command's output displays the permissions, ownership, size, created or last accessed date/time, and the filename:

ls7

Use ls4 to update the last accessed timestamp:

ls9

ls6

$1

Using ls4 to update last accessed time is actually an infrequent use of this command. The common use for ls4 is to create an empty file as a placeholder. Some programs require that a file exists to operate correctly, and this is one method of kickstarting such a process. Otherwise, this use offers a quick way to create a file without opening a text editor and then saving an empty file:

$4

ls6

$6

You have created a new empty file, $7.

$8 prints the working directory

The $8 command is your Linux system's compass, in that it tells you where you are. It has no other function than supplying that bit of information to you. Try the following, and you will see that you're in your home directory, which is shown in the format ls0:
ls1

If you get lost, or just wonder where you are in the filesystem, this is the command that will tell you. Linux users use it frequently before changing or removing files to be sure of their current location.

The $8 command always displays the full path to your location, even if you're multiple directories deep from the root (ls3) directory, which is why I see ls4 rather than ls5 or ls6.

 

ls7 changes directory

Very closely related to the $8 command is the ls7 command. Changing directories is a frequent activity on a Linux system. As stated before, when you first log in, you're placed into your home directory. Every user on a Linux system has a home directory. Regular user accounts have personal directories under the /etc0 directory. Your home directory is under ls0. To view all user's home directories, ls7 to the /etc0 directory.

/etc4

/etc5

What you see here depends on your system. If you are the only user on a personal system, you will only see your home directory. Production systems might have hundreds of user accounts on them. The quick way to return to your home directory, no matter where you are on the system, is to type ls7 with no arguments or directory paths:

/etc7

So, if you ever get "lost" on the system and need to reset your bearings, type ls7 and you'll be placed safely into your home directory. You can ls7 to almost any directory on the system by supplying its full path after the ls7 command:

/etc1

To change directory to the one above your current directory, use the double period (dot) argument:

/etc2

Now you are in the /etc3 directory. Remember that you can "prove" your location to yourself by issuing the $8 command:

/etc5

There are times when you don't need to ls7 to a particular directory. You can read a file from your current location if you supply the full path to the file you're interested in viewing. For example, you don't need to ls7 to the /etc8 directory to issue the $8 command. You issue it from your current location because it is in your path.

The path is a more advanced topic for another article, but just be aware that you don't need to ls7 to do everything. The time to ls7 is when you will be working in a specific directory for some reason. Otherwise, you can do what you need to do from your home directory. You'll find out why changing directories can be a bad thing in the next section.

$ ls /etc2 removes files and directories

The $ ls /etc2 command removes (deletes) files and directories. One of the quirks of Linux that you'll find different from DOS/Windows is that it isn't chatty, which means that when you remove a file or directory, you won't (by default) receive a message such as, "Are you sure?" It just isn't the Linux way. There is a recommended workaround for that behavior that I'll show you later in this section.

For now, let's remove the $7 file that you created earlier with the ls4 command:

$ ls /etc6

Did you notice that you didn't receive any questions or prompts? Linux assumes you know what you want to do before you hit the Enter key. That's a little disconcerting, isn't it? Ask Linux system administrators if any files have ever gone missing during one of their sessions. I'll put money on an affirmative response and I'm not a gambler. You can work around this non-interactive behavior of certain commands by placing a $ ls /etc7 switch (option) after the command. Try the following example:

$ ls /etc8

$ ls /etc9

The $ ls /etc7 makes $ ls /etc2 interactive. Answer with a /etc2 and the file goes away. Answer with an /etc3 and you keep the file. To be safe, you can always use the $ ls /etc7 switch with $ ls /etc2. There's no harm or shame in it and you'll be glad you did at some point in the future.

/etc6 copies files and directories

Copying files and directories is a very common task for Linux system administrators. There's no great secret to its usage and you simply issue the copy (/etc6) command, the file or directory source, and the destination. To copy a file, /etc8, to the /etc9 directory, use:

ls00

To copy an entire directory and all its contents, including subdirectories, use the ls01 (Recurse) option. Copy the data directory in your home directory to /etc9. You can use either the ls03 or ls01 to recurse copy files:

ls05

The /etc6 command is rare in that both the upper- and the lowercase options for an action are the same. Of course, you can use wildcards when copying files to filter them with patterns:

ls07

ls08 makes directories

If you're an organized person, you'll want to create directories to satisfy your need to properly arrange your files and data into separate compartments (directories). It's easy to create directories. Issue the ls08 command followed by the directory name you wish to create:

ls10

If you're even more organized and you've done some planning, you can create a whole hierarchy of directories with one command. You want to create a data directory that includes subdirectories for documents, forms, tests, and outgoing. Why issue multiple commands when you can do it all at once:

ls11

The ls12 option tells the system that you are creating a parent directory and subdirectories. Check your work using the ls command. You can also create multiple directories at the same level all at once.

ls14

Use the ls command to be sure the ls08 command did what you wanted it to do.

ls17 lists the current running processes

The last of the 10 basic Linux commands you need to know is ls17. This command shows you currently running processes. If you issue the ls17 command, you will only see your own processes:

ls20

If you're not running anything, then this output is not very interesting. It's far more interesting to see what's going on system-wide. You can do this by adding some options to ls17. The most valuable options are ls22 and ls23, for every (all) and full format, respectively. To get the most information from the ls17 command, combine the two options into the following command. I've included the first few lines from the output of ls25 from my system for you:

ls26

ls27

The fields are simple to understand and useful when troubleshooting performance problems:

FieldDescriptionCCPU Usage.CMDThe command or process name with path.PIDProcess ID.PPIDParent Process ID: The parent process is the one that spawned the process.STIMEStart Time for the process.TIMECPU Time for the process.TTYThe user terminal that spawned the process. System process will show a ?.UIDUser ID of the process owner.

 

There are other options you can use with the ls17 command, and it seems everyone has a preference, but the two most popular are: ls25 and ls30. They both provide you with a lot of process information.

There you have the 10 basic Linux commands you need to know. There isn't one command that's more important than any other. They're all important and they're all useful. I chose these because they are the 10 commands that everyone regularly uses whether you're a fresh newbie or an old salty system administrator from the dark days before Linux.

If you're interested in how DOS and Linux commands compare, check out this: Comparison of Common DOS and Linux Commands.

[Want to try out Red Hat Enterprise Linux? Download it now for free.]

Topics:   Linux  

What command enables you to find all empty files in your source directory?

Ken Hess

Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization. More about me

Red Hat Summit 2022: On Demand

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

Register for free

Image

What command enables you to find all empty files in your source directory?

Learn how Unix influenced Linux, understand Ansible lists and dictionaries, and more tips for sysadmin

Check out Enable Sysadmin's top 10 articles from November 2022.

Posted: December 5, 2022

Author: Vicki Walker (Editorial Team, Red Hat)

Image

What command enables you to find all empty files in your source directory?

6 ways to get information about your CPU on Linux

Getting insight into what CPU you're running is a useful trick to know.

Posted: December 5, 2022

Author: Seth Kenlon (Editorial Team, Red Hat)

Image

What command enables you to find all empty files in your source directory?

Find anything you need with fzf, the Linux fuzzy finder tool

I'm thankful for the Linux fuzzy finder tool because it superpowers the command line by making it fast to find whatever I'm looking for.

Which command is used for making an empty directory?

Use the mkdir command to create one or more directories specified by the Directory parameter.

Which command is used to empty file?

Using the truncate command.

Which command will search and list empty directories?

Use the 'find' command along with the '-type' flag that specifies the directory type search using the keyword 'd'. The word '-empty' has been used as a flag to search only empty directories within the home directory as stated below. The dot means the current location which is the home directory of a Linux-based system.

How to check empty file in Linux?

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data.