How to clear file in linux

Linux / UNIX: How To Empty Directory

How To Empty Directory In Linux and Unix

  1. rm command – Delete one or more files or directories.
  2. find command – Find and delete all files from a specific directory.

Linux Empty Directory Using the rm Command

First, consider the following directory structure displayed using the tree command

To delete all files from /tmp/foo/ directory (i.e. empty /tmp/foo/ directory), enter:
$ cd /tmp/foo/
$ rm *
OR
$ rm /tmp/foo/*

Delete All Files Using the Find Command

Consider the following directory structure:

To delete all files from /tmp/bar/ directory (including all files from sub-directories such as /tmp/bar/dir1), enter:
$ cd /tmp/bar/
$ find . -type f -delete
OR
$ find /tmp/bar/ -type f -delete
The above find command will delete all files from /tmp/bar/ directory. It will not delete any sub-directories. To remove both files and directories, try:
find /path/to/target/dir/ -delete
The find commands options are as follows:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

  • -type f : Delete on files only.
  • -type d : Remove folders only.
  • -delete : Delete all files from given directory name.

How to remove a full directory and all files in Linux

To remove a directory that contains other files or sub-directories, use the following rm command command. In the example, I am going to empty directory named “docs” using the rm -rf command as follows:
rm -rf /tmp/docs/*
Get verbose outputs:
rm -rfv /tmp/docs/*

The rm command options are as follows:

  • -r : Delete directories and their contents recursively on Linux or Unix-like systems.
  • -f : Forceful removal. In other words, ignore nonexistent files and delete whatever found.
  • -v : Verbose outputs. For example, explain what is being done on screen.

Conclusion

You learned how to use the rm and find command to delete all files and sub-directories on Linux/macOS/*BSD and Unix-like systems. In other words, this is useful to empty folders on Linux. For more information see rm command help page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How To Clear .Cache Folders and Free Up Space On Your Linux PC

Clean up your cache in a few easy steps!

GNU/Linux has implemented efficient storage management for its users. But have you noticed your Linux system running out of space, filled with unused packages you installed months or years ago? How do find them and remove them? Here are a few valuable tips!

Cache File Location

Cache files are stored in /home/username/.cache which mostly consists of your browser’s data, IDE’s (if you use any ) and other software. Each user has its own data and this can build up exponentially . In order to clear it all it’s recommended to have Disk usage analyzer installed.

Disk Usage Analyzer

Disk Usage Analyzer, formerly known as Baobab, is a graphical disk usage analyzer for the GNOME desktop environment. It was part of gnome-utils, but has been a standalone application since GNOME 3.4.

Читайте также:  Как скомпилировать ядро линукса

The software gives the user a graphical representation of a disk drive’s contents. The interface allows for selection of specific parts of filesystem so a single folder, the entire filesystem, and even remote folders and filesystems can be scanned and listed at the folder level.

The graphical representation can be switched between a ‘Rings’ chart and a ‘Treemap’ chart to better suit the content being viewed.

Installation

If you don’t already have Disk Usage Analyzer installed, you first need to install the baobab package:

Clear Cache files and folders

Click on home for your user data .

Click on your second disk i.e Vostro-14-3468 as shown in the image below .

Note: Names will be different on your own computer.

Here, the other disks shown as New Volume / 262 GB volume are formatted disks for a dual boot system, in my case Ubuntu 20.10 and Windows 10.

As you click on it, the Disk Usage Analyzer will start to scan your directory for files and folders. Be patient and allow it to finish. After Disk Usage Analyzer is done scanning your Linux PC for files it shows the live illustration of Linux disk usage as the form of a pie chart.

To see updated changes in your filesystem, you’ll need to refresh the scan as it won’t track these storage changes “live.”

Look in the folder tree structure for .cache, and click on it .

Once you click on the .cache folder, the disk usage analyzer will show you the space it’s consuming graphically on the right side .

  • Find the sub folders inside it which you wish to delete .
  • Right-click each one
  • Select Move to trash / bin
  • Now empty your trash / bin

And you are done!

Do not worry about deleting these folders. It is totally safe and the software will recreate them if and when it needs to.

WARNING!

Do not delete anything from your usr and var directories. usr is used for “user programs”. Usually your package manager installs all the binaries, shared files etc. from all programs here (except config files, which go to /etc).

You can check /usr/bin for binaries, /usr/share for shared files (media, etc), /usr/share/doc for documentation.

There is also an /opt folder, where there are “other” binary programs or programs installed from sources other than the default package manager. Some programs like that (usually compiled) also go to usr/local

/var is usually used for log files, ‘temporary’ files (like mail spool, printer spool, etc), databases, and all other data not tied to a specific user.

Logs are usually in /var/log databases in var/lib (mysql — var/lib/mysql ), etc.

If you liked this article, please follow the author on medium !

Источник

5 Ways to Empty or Delete a Large File Content in Linux

Occasionally, while dealing with files in Linux terminal, you may want to clear the content of a file without necessarily opening it using any Linux command line editors. How can this be achieved? In this article, we will go through several different ways of emptying file content with the help of some useful commands.

Caution: Before we proceed to looking at the various ways, note that because in Linux everything is a file, you must always make sure that the file(s) you are emptying are not important user or system files. Clearing the content of a critical system or configuration file could lead to a fatal application/system error or failure.

With that said, below are means of clearing file content from the command line.

Important: For the purpose of this article, we’ve used file access.log in the following examples.

1. Empty File Content by Redirecting to Null

A easiest way to empty or blank a file content using shell redirect null (non-existent object) to the file as below:

Empty Large File Using Null Redirect in Linux

2. Empty File Using ‘true’ Command Redirection

Here we will use a symbol : is a shell built-in command that is essence equivalent to the true command and it can be used as a no-op (no operation).

Another method is to redirect the output of : or true built-in command to the file like so:

Empty Large File Using Linux Commands

3. Empty File Using cat/cp/dd utilities with /dev/null

In Linux, the null device is basically utilized for discarding of unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by redirection mechanism.

Читайте также:  Windows сохранить список файлов

And the /dev/null device file is therefore a special file that writes-off (removes) any input sent to it or its output is same as that of an empty file.

Additionally, you can empty contents of a file by redirecting output of /dev/null to it (file) as input using cat command:

Empty File Using cat Command

Next, we will use cp command to blank a file content as shown.

Empty File Content Using cp Command

In the following command, if means the input file and of refers to the output file.

Empty File Content Using dd Command

4. Empty File Using echo Command

Here, you can use an echo command with an empty string and redirect it to the file as follows:

Empty File Using echo Command

Note: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means non-existence of an object.

For this reason, when you redirect the out of the echo command above into the file, and view the file contents using the cat command, is prints an empty line (empty string).

To send a null output to the file, use the flag -n which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.

Empty File Using Null Redirect

5. Empty File Using truncate Command

The truncate command helps to shrink or extend the size of a file to a defined size.

You can employ it with the -s option that specifies the file size. To empty a file content, use a size of 0 (zero) as in the next command:

Truncate File Content in Linux

That’s it for now, in this article we have covered multiple methods of clearing or emptying file content using simple command line utilities and shell redirection mechanism.

These are not probably the only available practical ways of doing this, so you can also tell us about any other methods not mentioned in this guide via the feedback section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Empty or Delete a log files in Linux or UNIX

How to clear the contents of a log file from the command line

Say you want to clear the contents of a log file named /var/log/messages, run:
# >/var/log/messages
The following is compatible with various Linux or Unix shells:
: > /var/log/messages
Verify file size:
# ls -l /var/log/messages
If you really wanted to delete or remove a file type the following rm command:
# rm /var/log/message

Delete a log files in Linux or UNIX using truncate

Use the truncate command to shrink or extend the size of each FILE to the specified size. So a proper way to clear log file named www.cyberciti.biz_access.log is to run the following command:
# cd /var/log/nginx/
# ls -lh www.cyberciti.biz_access.log
# truncate -s 0 www.cyberciti.biz_access.log
# ls -lh www.cyberciti.biz_access.log

Clear the contents of a file named /var/log/nginx/www.cyberciti.biz_access.log from the command line

Other commands to empty or delete a large file content in Linux

Try the cat command:
cat /dev/null > www.cyberciti.biz_access.log
Or the cp command:
# cp /dev/null /var/log/nginx/php_error.log

How do I clear log file using dd on Linux or Unix?

Type dd command as follows:
# dd if=/dev/null of=/path/to/log/file
# dd if=/dev/null of=/var/log/lighttpd/error_log

How to empty or truncate a file in Linux using echo/printf

One more method is to use the echo command:
# echo -n «» > /path/to/java/appserver.log

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard
Читайте также:  Windows ce arm mips sh4 как узнать

Join Patreon

Say hello to logrotate tool

A better approach is to use logrotate tool. It is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large. See my previous logrotate help page:

Conclusion

This page showed how to empty or delete a large log file contents in Linux and Unix like systems. There are other methods too as discussed in the comments section below.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

my log files /var/log deleted is it creates any problem

/var/log is directory, if you delete the same just recreate directory using mkdir command:
mkdir /var/log

How would you specify to delete log that are over a certain age. ie 2days old or something like that. Thanks for the help.

One way is to setup new cronjob for that. Then google: “tmpwatch delete files older”

There are 2 options, either use log logrotate or use find command to get list of 2 days old files and empty them. You can find information about logrotate and about find command here only. Use search box to get information

Thank you for pointing me in the right direction. I’ve been doing some more research and had another question if you wouldn’t mine throwing in some input.

I want to remove apache logs after about a month. So I was going to set something up like this that I found online.

This is an example of a /etc/logrotate.d/httpd

/var/log/httpd/*.log <
daily
rotate 30
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true endscript
>

Now from what I understand this will rotate the log daily meaning create a new log everyday. The 30 means that it will hold 30 of these logs before deleting them (or compressing if I put that option in there)

Also, before I go and set this up, I want to test it on 1 websites log incase I mess things up. Would it be ok to replace the line

/var/log/httpd/*.log
with
/var/log/httpd/”website”*.log to that affect to test in one website before doing all of them?

Does it sound like I’m on the right track or am I completely lost 🙂 thanks again for your input.

simply wirte rm file.log and press enter

Use the following command:

suppose your file name is message.log then use:

This will empty the file without having deleting it.

Hallo, i have same use command as your suggest.
echo ”> message.log

but, if one folder contain much log like that, how i can clean up directly using echo command without delete it (rm)

Hi Ajeet,
Can you tell me please how it works ?

echo “” ->print the blank space
and that blank space is redirected to message.log file
or in other word your file conatins replaced by blank space without

Please could anybody help me how to delete the log files of DHCP server. and how to identify them.

Источник

Оцените статью