Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to sort lines in text files in Linux | sort Command
Next article icon

shred Command in Linux with Examples

Last Updated : 10 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your hard drive, and it could be recovered. When you delete a file permanently or delete it from the trash, the pointer pointing to the file leaves the address of it and the data of the file is sent to a sector in hard disk and is considered as unallocated space and it can be recovered easily. The file gets permanently deleted when the OS writes over the sector of the file which was considered as unallocated. So, in order to delete a file completely from a hard disk “shred” is used in Linux. This command overwrites the contents of a file multiple times, using patterns chosen to maximize the destruction of the residual data, making it harder for even very expensive hardware probing to recover it. 

Syntax of the `shred` command in Linux

shred [OPTION] FILE

[OPTIONS] represents the various parameters and flags that can be used to modify the behavior of the Shred command

FILE refers to the file or files you wish to shred.

Options available in `shred` Command

Options Description
-n, –iterations=N

This option allows you to specify the number of times the file will be overwritten during the shredding process. By default, Shred performs 3 iterations.

-u, –remove

This option instructs Shred to remove the file after the shredding process is complete.

-v, –verbose

When using this option, Shred provides detailed information about the shredding process.

-z, –zero

This option adds a final overwrite of all zeros to the file after the shredding process is complete. This helps to hide the fact that the file has been shredded.

-f, –force

This option forces Shred to shred files that have read-only permissions or are otherwise protected.

-r, –random-source=FILE

With this option, you can specify a file as the source of random data for overwriting the file being shredded.

Examples and Usage of the `shred` command in Linux

1. Overwrite the contents of the file multiple times to make it unrecoverable.

shred filename.txt

 The `filename.txt` will be overwritten during the shredding process.

To-overwrite-the-contents-of-the-file-multiple-times-to-make-it-unrecoverable

To-overwrite-the-contents-of-the-file-multiple-times-to-make-it-unrecoverable

It will change the file data in such a way that it would be really hard to get the old file back. 

Note: In this case, The name of the file is filename.txt you may change it as per your need. 

2. `-n, –iterations=N` option in `shred` command 

To change the number of times a file is to be overwritten. By default, Shred performs 3 iterations but with this option we can define the number of iterations.

shred -n 10 filename.txt
To-change-the-number-of-times-a-file-is-to-be-overwritten

To-change-the-number-of-times-a-file-is-to-be-overwritten

 This command will overwrite the file 10 times.

 Note: In this case, the number of times the file is to be shredded is set to be 10 and the name of the file is filename.txt you may change these as per your need. 

3. `-u, –remove` option in `shred` command 

 To remove the file after the shredding process is complete.

shred -u filename.txt
To-overwrite-and-delete-a-file-as-well

To-overwrite-and-delete-a-file-as-well

 This will overwrite the file many times and will delete it as well. 

Note: In this case, The name of the file is filename.txt you may change it as per your need.

 4. To overwrite some specific bytes of text only.

shred -s 5 filename.txt
To-overwrite-some-specific-bytes-of-text-only

To-overwrite-some-specific-bytes-of-text-only

 This will overwrite the first 5 bytes of the file.

 Note: In this case, The name of the file is filename.txt and the number of bytes is 5, you may change these as per your need. 

5. `-v, –verbose` option in `shred` command 

To run shred command with verbose mode or to get how many times the file is overwritten

shred -v filename.txt
To-run-shred-command-with-verbose-mode

To-run-shred-command-with-verbose-mode

 It will display every time it overwrites the file. 

Note: In this case, The name of the file is filename.txt you may change it as per your need.

 6. `-f, –force` option in `shred` command 

To change permissions to allow writing if necessary while using shred command.

shred -f filename.txt
To-change-permissions-to-allow-writing-if-necessary-while-using-shred-command

To-change-permissions-to-allow-writing-if-necessary-while-using-shred-command

 When you run shred command with -f option it will write the file even by changing the permissions if necessary. 

Note: In this case, The name of the file is filename.txt you may change it as per your need. 

7. `-z, –zero` option in `shred` command

To add a final, overwrite with zeros to hide shredding.

shred -z filename.txt`
To-add-a-final-overwrite-with-zeros-to-hide-shredding

To-add-a-final-overwrite-with-zeros-to-hide-shredding

 After completing the shredding, it will overwrite the file with zeros to hide shredding.

 Note: In this case, The name of the file is filename.txt you may change it as per your need. 

8. `–version` option in `shred` command 

To get basic details and version of shred command.

shred --version
To-get-basic-details-and-version-of-shred-command

To-get-basic-details-and-version-of-shred-command

 This will display the version of the shred command present in your system along with some copyright details.

Conclusion

In this article we have discussed about `shred` command in Linux which provides a reliable and effective way for securely deleting files that can’t be recovered further. We learn that overwriting file content multiple times ensures that our data cannot be easily retrieved. We have also discussed various options in `shred` command.



Next Article
How to sort lines in text files in Linux | sort Command
author
manav014
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-system-commands

Similar Reads

  • screen command in Linux with Examples
    The screen command is an advanced terminal multiplexer that allows you to have multiple sessions within one terminal window. It's like having "tabs" in your Linux terminal — you can open, detach, switch, or resume sessions at any time without losing what you're working on. It's particularly convenie
    7 min read
  • script command in Linux with Examples
    The 'script' command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything disp
    6 min read
  • scriptreplay command in Linux with Examples
    scriptreplay command is used to replay a typescript/terminal_activity stored in the log file that was recorded by the script command. With the help of timing information, the log files are played and the outputs are generated in the terminal with the same speed the original script was recorded. The
    3 min read
  • sdiff command in Linux with Examples
    sdiff command in Linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays a greater than sign if the line only exists in the file spe
    3 min read
  • Sed Command in Linux/Unix With Examples
    The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing. With SED, you can manipulate text files wi
    9 min read
  • select command in Linux with examples
    select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of commands written in the select block and then asks again to enter a number, if a wrong option is entered it does nothing. If the user enters
    3 min read
  • seq command in Linux with Examples
    The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to a
    4 min read
  • setsid command in Linux with Examples
    setsid command in Linux system is used to run a program in a new session. The command will call the fork(2) if already a process group leader. Else, it will execute a program in the current process. The main advantage of using 'setsid' is that it allows programs to run independently of the terminal
    3 min read
  • Shift command in Linux with examples
    Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command
    3 min read
  • showkey command in Linux with Examples
    showkey command in Linux is used to examine the codes sent by the keyboard. showkey prints to standard output either the scan codes or the key code or the 'ASCII' code of each key pressed. The program runs in the first two modes until 10 seconds have elapsed since the last key press or release event
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences