Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

02 April 2026

How to find files? How to find directories?

How to find files? 

du -sk ./* | sort -nr

or 

du -sk ./* | grep -i "keyword" | sort -nr

or 

$ find . -maxdepth 1 -iname "*keyword*" -type f -printf "%T+ %p\n" | sort -r

or 

$ find . -maxdepth 1 -iname "*keyword*" -type f -exec du -sk {} + | sort -nr

 

How to find directories?

$ du -sk ./*/ | sort -nr

or

$ du -sk ./*/ | grep -i "keyword" | sort -nr 

or

$ find . -maxdepth 1 -iname "*keyword*" -type d -not -path '.' -printf "%T+ %p\n" | sort -r

or

$ find . -maxdepth 1 -iname "*keyword*" -type d -not -path '.' -exec du -sk {} + | sort -nr 

 

20 March 2026

How to find the most largest 10 files on Ubuntu?

To find the largest 10 files in a directory recursively on Ubuntu, use a combination of the
find, du, sort, and head commands.
Command to find the 10 largest files (human-readable format)
Run the following command in your terminal. Replace . with the specific directory path if you want to search a different location:
bash
find . -type f -exec du -Sh {} + | sort -rh | head -n 10
Explanation of the Command
Here is a breakdown of the commands and options used:
  • find .: Starts the search from the current directory (.) and traverses all subdirectories recursively.
  • -type f: Restricts the search to regular files only, ignoring directories, links, etc..
  • -exec du -Sh {} +: Executes the du command on the found files.
    • du: Estimates file space usage.
    • -S: Reports the size of individual files, not including the size of subdirectories (important for listing individual files correctly).
    • -h: Displays sizes in a human-readable format (e.g., 10MB, 2G).
    • {} and +: These are used by find to pass multiple filenames to a single du command efficiently.
  • |: Pipes the output of the find command to the sort command.
  • sort -rh: Sorts the output lines.
    • -r: Reverses the sort order, placing the largest files at the top.
    • -h: Compares human-readable numbers correctly (e.g., understands that 2G is larger than 10M).
  • | head -n 10: Displays only the first 10 lines of the sorted output (the top 10 largest files).
Alternative: Using Bytes for Precise Sorting
If you need a very precise numeric sort (e.g., if the human-readable sorting behaves unexpectedly on non-GNU systems), you can print sizes in bytes:
bash
find . -type f -printf "%s %p\n" | sort -nr | head -n 10
This command prints the size in bytes (%s) followed by the file path (%p), sorts numerically (-n) in reverse order (-r), and shows the top 10 results.
Interactive Method (GUI/TUI)
For an interactive, user-friendly experience, you can use the ncdu tool, which is available in the Ubuntu package manager.
  1. Install ncdu: sudo apt install ncdu.
  2. Run in the directory: ncdu . (or a specific path, e.g., ncdu /var/log). This provides an interactive interface to navigate directories and see the largest files and folders.

05 March 2025

How To Search And Replace String Across Multiple Files in Vim

https://irian.to/blogs/how-to-search-and-replace-string-across-multiple-files-in-vim

 

Perform substitution with macros and repeat it

While recording a macro, perform substitution on one file, and repeat the macros across all args.

Assuming the same folder structure and args, here is how it is done:

qq                                          // start macro in q register
%s/stringToBeReplaced/replacementString/ge  // the e flag tells vim to not throw an error if there is no match
:wnext                                      //important. This is similar to `:next`, but it also writes the current file
q                                           // stop macro
999@q                                       //repeat this macros either 999 times or to remaining files.

That's it!

08 March 2023

How to install .deb files on Ubuntu?

https://linuxconfig.org/install-deb-file-on-ubuntu-22-04-jammy-jellyfish-linux


$ sudo dpkg -i example.deb
 
If the package requires dependencies that aren’t already on your system, you will see 
this error in the terminal:
 
dpkg: error processing package 

Don’t fret, because we can install the required dependencies with a single command, 
assuming that they are available in Ubuntu’s package repository. Just run this command 
to install the dependencies: 

$ sudo apt install -f

22 August 2021

Can open Excel files locally but not from network

 

Can open Excel files locally but not from network

Problem: 

Can open Excel files locally but not from network

Solution:

File -> Options -> Trust Centre -> Trust Centre Settings -> Protected View.

Clear all the ticks. Click OK.



10 August 2021

How to read a file, which only contains a string, into Python, and then convert the string into a list?

 How to read a file, which only contains a string, into Python, and then convert the string into a list?


from pathlib import Path

txt = Path('filename.txt').read_text()

list_ = list(txt.split('; '))

27 July 2021

How to concatenate all the text files in a directory with Python?

 How to concatenate all the text files in a directory with Python?


https://stackoverflow.com/questions/13613336/python-concatenate-text-files

import glob2

filenames = glob2.glob('*.txt')  # list of all .txt files in the directory

with open('outfile.txt', 'w') as f:
    for file in filenames:
        with open(file) as infile:
            f.write(infile.read()+'\n')

09 July 2021

How to get lost files back resulting from running git add and git restore -s@ -SW?

 How to get lost files back resulting from running git add and git restore -s@ -SW?


Problem: 

suppose you ran

$ git add .

then you ran

$ git status

and saw that you have added some files which you did not want to add. 

(You did not run 

$ git commit

)

then you wanted to remove them from the index.

In haste, you ran 

$ git restore -s@ -SW

resulting in those files are gone away from your working tree (resulted from -W).

You want to get them back.

Solution:

Run

$ git fsck --cach --no-reflogs --lost-found --unreachable head

You will get a list of SHA1, which point to your lost files.

Then run

$ git show SHA1 > name_of_lost_file

one by one, every lost file will now be in your current work directory!


01 July 2021

How to find all the files containing the string 'abc' in the current directory and all the sub-directories

How to find all the files containing the string 'abc' in the current directory and all the sub-directories?


find . -type f | xargs fgrep '...'

20 June 2021

How to list all html files in a directory recursively sorted by file size?

 How to list all html files in a directory recursively sorted by file size?

$du -a -h | sort -nr | grep 'html' | less

Note:

1. '-a' means all files and directories, not just directories.

2. '-h' means human readable.

3. '-n' means numeric sort.

4. '-r' means recursive.

24 March 2021

How to redirect 'print' output to a file using python?

 How to redirect 'print' output to a file using python?




import sys

orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f

for i in range(2):
    print 'i = ', i

sys.stdout = orig_stdout
f.close()

29 July 2019

How can I find out if a specific program is installed?

How can I find out if a specific program is installed?



scp [source file] [username]@[destination server]:.


scp cool_stuff.txt sanjeev@example.com:.


 Remember not to miss the colon and full stop at the end.

https://superuser.com/questions/850740/how-to-upload-local-file-to-server-through-linux-terminal






23 March 2018

How to associate .docx with Microsoft Word in Windows 10?

How to associate .docx with Microsoft Word in Windows 10?

Open 'File Explorer'.
Browse to the .docx file you want to open but failed.
Select it by clicking it.
Click 'Home' tab in the menu on the top.
Click the downward triangle beside 'Open'.
Click 'Choose another app'.
Click 'More apps ↓'.
If there is no 'Microsoft Word', then scroll down and click 'Look for another app on this PC'. An 'Open with...' dialogue box' pops up. Current path would be 'Windows (C:) > Program Files'.
Double-click 'Microsoft Office'.
Double-click 'Office14'.
Choose 'WINWORD.EXE'. Click 'Open' button. (Tick 'Always use this app to open .docx files'.)
Done.