Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [files]

Manipulating files: copying, renaming, searching, analyzing, archiving, etc. For operating on text in a file, see /text-processing. For questions about the Files file manager (formerly Nautilus), see /nautilus.

232
votes
12answers
350k views

How do I loop through only directories in bash?

I have a folder with some directories and some files (some are hidden, beginning with dot). for d in *; do echo $d done will loop through all files, but I want to loop only through directories. How ...
225
votes
10answers
467k views

How to list all files ordered by size

I would like to list all files in the order of big to small in size and the files could be present anywhere in a certain folder.
219
votes
14answers
392k views

How can I get the size of a file in a bash script?

How can I get the size of a file in a bash script? How do I assign this to a bash variable so I can use it later?
212
votes
3answers
365k views

Empty the contents of a file

I am aware of three methods to delete all entries from a file. They are >filename touch filename filename < /dev/null Of these three I abuse >filename the most as that requires the least ...
208
votes
9answers
604k views

How to get over “device or resource busy”?

I tried to rm -rf a folder, and got "device or resource busy". In Windows, I would have used LockHunter to resolve this. What's the linux equivalent? (Please give as answer a simple "unlock this" ...
197
votes
3answers
234k views

How to tell gzip to keep original file?

I would like to compress a text file using gzip command line tool while keeping the original file. By default running the following command gzip file.txt results in modifying this file and renaming ...
171
votes
3answers
494k views

Get file created/creation time? [duplicate]

Possible Duplicate: How do I do a ls and then sort the results by date created? Is there a command in Linux which displays when the file was created ? I see that ls -l gives the last modified ...
165
votes
6answers
17k views

Why is looping over find's output bad practice?

This question is inspired by Why is using a shell loop to process text considered bad practice ? I see these constructs for file in `find . -type f -name ...`; do smth with ${file}; done and for ...
161
votes
4answers
691k views

Where are Apache file access logs stored?

Does anyone know where file access logs are stored, so I can run a tail -f command in order to see who is accessing a particular file. I have XAMPP, which is an Apache server installed on my machine, ...
153
votes
11answers
185k views

How can I edit multiple files in VIM?

I know I can open multiple files with vim by doing something like vim 2011-12*.log, but how can I switch between files and close the files one at a time? Also, how can I tell the file name of the ...
151
votes
16answers
220k views

Efficiently delete large directory containing thousands of files

We have an issue with a folder becoming unwieldy with hundreds of thousands of tiny files. There are so many files that performing rm -rf returns an error and instead what we need to do is something ...
148
votes
6answers
79k views

What's the point in adding a new line to the end of a file?

Some compilers (especially C or C++ ones) give you warnings about: No new line at end of file I thought this would be a C-programmers-only problem, but github displays a message in the commit view: ...
133
votes
4answers
383k views

Delete files older than X days +

I have found the command to delete files older than 5 days in a folder find /path/to/files* -mtime +5 -exec rm {} \; But how do I also do this for subdirectories in that folder?
131
votes
19answers
43k views

Is there an easy way to replace duplicate files with hardlinks?

I'm looking for an easy way (a command or series of commands, probably involving find) to find duplicate files in two directories, and replace the files in one directory with hardlinks of the files in ...
131
votes
2answers
577k views

Unzipping a .gz file without removing the gzipped file [duplicate]

I have a file file.gz, when I try to unzip this file by using gunzip file.gz, it unzipped the file but only contains extracted and removes the file.gz file. How can I unzip by keeping both unzipped ...
130
votes
9answers
187k views

Find the total size of certain files within a directory branch

Assume there's an image storage directory, say, ./photos/john_doe, within which there are multiple subdirectories, where many certain files reside (say, *.jpg). How can I calculate a summary size of ...
127
votes
6answers
371k views

How can I count the number of lines of a file with common tools? [duplicate]

I am redirecting grep results to a file, and then using cat to show its contents on the screen. I want to know how many lines of results I have in my results file and then add it to some counter. ...
127
votes
6answers
281k views

How do I grep recursively through .gz files?

I am using a script to regularly download my gmail messages that compresses the raw .eml into .gz files. The script creates a folder for each day, and then compresses every message into its own file. ...
124
votes
5answers
308k views

Sort based on the third column

I'm facing a huge 4-columns file. I'd like to display the sorted file in stdout based on its 3rd column: cat myFile | sort -u -k3 Is that enough to perform the trick?
123
votes
9answers
168k views

How to remove duplicate lines inside a text file?

A huge (up to 2 GiB) text file of mine contains about 100 exact duplicates of every line in it (useless in my case, as the file is a CSV-like data table). What I need is to remove all the repetitions ...
123
votes
1answer
91k views

What is the difference between modify and change in stat command context?

The stat command's manual page says: %x Time of last access %y Time of last modification %z Time of last change I cannot understand the difference between modify and change. I ...
122
votes
4answers
51k views

locate vs find: usage, pros and cons of each other

In Linux and Unix systems there are two common search commands: locate and find. What are the pros and cons of each? When one have benefits over the other?
121
votes
8answers
253k views

List only regular files (but not directories) in current directory

I can use ls -ld */ to list all the directory entries in the current directory. Is there a similarly easy way to just list all the regular files in the current directory? I know I can use find find . ...
121
votes
8answers
645k views

How can I display the contents of a text file on the command line?

I would like to display the contents of a text file on the command line. The file only contains 5-6 characters. Is there an easy way to do this?
120
votes
5answers
117k views

What is a Superblock, Inode, Dentry and a File?

From the article Anatomy of the Linux file system by M. Tim Jones, I read that Linux views all the file systems from the perspective of a common set of objects and these objects are superblock, inode, ...
118
votes
5answers
100k views

Clone ownership and permissions from another file?

Is there a command or flag to clone the user/group ownership and permissions on a file from another file? To make the perms and ownership exactly those of another file?
114
votes
9answers
20k views

Why is /dev/null a file? Why isn't its function implemented as a simple program?

I am trying to understanding the concept of special files on Linux. However, having a special file in /dev seems plain silly when its function could be implemented by a handful of lines in C to my ...
114
votes
10answers
222k views

Script to monitor folder for new files?

How to detect new files in a folder with a bash script? I would like to process the files as soon as they are created in the folder. Is this possible to do so or do I have to schedule a script with ...
110
votes
7answers
36k views

How are “/dev” Linux files created?

There are special files in Linux that are not really files. The most notable and clear examples of these are in the dev folder, "files" like: /dev/null - Ignores anything you write to the file /dev/...
110
votes
3answers
201k views

Find and remove large files that are open but have been deleted

How does one find large files that have been deleted but are still open in an application? How can one remove such a file, even though a process has it open? The situation is that we are running a ...
108
votes
5answers
157k views

How do I do a ls and then sort the results by date created?

In what order are the dated ordered by? Certainly not alphanumeric order. ls -lt sorts by modification time. But I need creation time.
102
votes
5answers
125k views

Generate File of a certain size?

I'd like to generate a file with the name example.file. I could use touch example.file but I want the file to be exactly 24MB in size. I already checked the manpage of touch, but there is no ...
102
votes
7answers
290k views

How to find creation date of file?

I want to find out the creation date of particular file, not modification date or access date. I have tried with ls -ltrh and stat filename.
101
votes
2answers
237k views

timestamp, modification time, and created time of a file

I just know that ls -t and ls -f give different sorting of files and subdirectories under a directory. What are the differences between timestamp, modification time, and created time of a file? How ...
100
votes
1answer
147k views

How to skip “permission denied” errors when running find in Linux? [duplicate]

Possible Duplicate: How do I remove “permission denied” printout statements from the find program? When I run this command in Linux (SuSE): find / -name ant I get many error ...
91
votes
3answers
56k views

Pseudo files for temporary data

I often want to feed relatively short string data (could be several lines though) to commandline programs which accept only input from files (e.g. wdiff) in a repeated fashion. Sure I can create one ...
89
votes
7answers
61k views

Find duplicate files

Is it possible to find duplicate files on my disk which are bit to bit identical but have different file-names?
88
votes
3answers
69k views

Why can rm remove read-only files?

If I create a file and then change its permissions to 444 (read-only), how come rm can remove it? If I do this: echo test > test.txt chmod 444 test.txt rm test.txt ...rm will ask if I want to ...
87
votes
4answers
141k views

last time file opened

Is it possible to get the time when file was opened last time and sort all files in a directory by those times?
86
votes
4answers
16k views

Why does Linux use LF as the newline character?

As far as I know, every operating system has a different way to mark the end of line (EOL) character. Commercial operating systems use carriage return for EOL (carriage return and line feed on Windows,...
85
votes
1answer
139k views

How to change where a symlink points [duplicate]

Can you tell me what this is in the area marked red? If I want to change /media/files/tb-prod/files to some other path, how would I do that?
84
votes
6answers
243k views

How to merge all (text) files in a directory into one?

I've got 14 files all being parts of one text. I'd like to merge them into one. How to do that?
83
votes
1answer
9k views

Why does the following script delete itself?

If you create an executable file with the following contents, and run it, it will delete itself. How does this work? #!/bin/rm
76
votes
5answers
106k views

Understanding of diff output

I have file1.txt this is the original text line2 line3 line4 happy hacking ! and file2.txt this is the original text line2 line4 happy hacking ! GNU is not UNIX if I do: ...
74
votes
5answers
769k views

How to create a simple .txt (text) file using terminal? [closed]

I'm just trying to review basic terminal commands. Having said that, how do I create a text file using the terminal only?
71
votes
5answers
110k views

What's the best way to join files again after splitting them?

If I have a large file and need to split it into 100 megabyte chunks I will do split -b 100m myImage.iso That usually give me something like xaa xab xac xad And to get them back together I have ...
65
votes
6answers
34k views

Flattening a nested directory

This is probably very simple, but I can't figure it out. I have a directory structure like this (dir2 is inside dir1): /dir1 /dir2 | --- file1 | --- file2 What is ...
63
votes
3answers
78k views

What are character special and block special files in a unix system? [duplicate]

How are character special files and block special files different from regular files in a Unix-like system? Why are they called “character special” and “block special” respectively?
62
votes
5answers
96k views

How to determine which process is creating a file? [duplicate]

Given file path, how can I determine which process creates it (and/or reads/writes to it)?
60
votes
3answers
83k views

grep returns “Binary file (standard input) matches” when trying to find a string pattern in file

I'm on Ubuntu and I typed cat .bash_history | grep git and it returned Binary file (standard input) matches My bash_history does exist and there are many lines in it that starts with git. What ...