Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

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.

23 February 2026

Knowing port number, how to kill a process which is using that port?

  • Find what's sitting on the port:  

    $ sudo lsof -i :8000 (Replace 8000 with your port)

  • Kill that specific listener:  

    $ sudo kill -9 <PID_FROM_LSOF>


If I am running -

$ gunicorn --bind 0.0.0.0:8000 app:app
[2026-02-23 10:38:25 -0500] [250191] [INFO] Starting gunicorn 25.1.0
[2026-02-23 10:38:25 -0500] [250191] [INFO] Listening at: http://0.0.0.0:8000 (250191)
[2026-02-23 10:38:25 -0500] [250191] [INFO] Using worker: sync
[2026-02-23 10:38:25 -0500] [250191] [INFO] Control socket listening at /home/shermanchen/dev/convShop2/gunicorn.ctl
[2026-02-23 10:38:25 -0500] [250193] [INFO] Booting worker with pid: 250193

 

When I run lsof, I get -

$ sudo lsof -i :8000                           
COMMAND     PID        USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME                            
gunicorn 250191 shermanchen    3u  IPv4 1077472      0t0  TCP *:8000 (LISTEN)                 
gunicorn 250193 shermanchen    3u  IPv4 1077472      0t0  TCP *:8000 (LISTEN) 

 

If I kill the 'booting worker' pid 250193, the gunicorn will not be killed.  I have to kill the 'listening' pid 250191, to kill the gunicorn process.

$ sudo kill -9 250191

  

If I use ps, I got

$ ps aux | grep gunicorn                       
sherman+  250690  0.0  0.0   9824  2172 pts/1    S+   10:45   0:00 grep --color=auto gunicorn 
 

Killing pid 250690 will not kill the gunicorn process.

01 February 2026

How to crop a video on Ubuntu?

ffmpeg -i input.mp4 -vf "crop=1200:626:279:89" -c:v libx264 -crf 20 -c:a copy output.mp4


To crop a video on Ubuntu, you use FFmpeg’s
crop filter. Cropping is different from resizing; it’s like taking a pair of scissors to the video frame to cut out a specific area (like removing a watermark or focusing on a specific person).

The logic for cropping is: crop=width:height:x:y


1. The Basic Crop Command

If you know the exact dimensions you want to keep, use this:

Bash
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0" -c:a copy output_cropped.mp4
  • 1280: The width of the "box" you want to keep.

  • 720: The height of the "box" you want to keep.

  • 0: The X coordinate (distance from the left edge) where the box starts.

  • 0: The Y coordinate (distance from the top edge) where the box starts.


2. Common "Real World" Examples

To crop a square from the center (great for social media):

Bash
ffmpeg -i input.mp4 -vf "crop=ih:ih" -c:a copy output_square.mp4

(Note: ih stands for "Input Height." This tells FFmpeg to make the width equal to the height.)

To cut the video in half (keep only the right side):

Bash
ffmpeg -i input.mp4 -vf "crop=iw/2:ih:iw/2:0" -c:a copy output_right_half.mp4

3. How to find your coordinates (The Easy Way)

Trying to guess "X" and "Y" coordinates in a terminal is hard. Here is a trick to do it visually:

  1. Open your video in VLC.

  2. Go to Tools > Effects and Filters.

  3. Go to Video Effects > Crop.

  4. Type in numbers until the video looks exactly how you want it.

  5. Write down those numbers (Left, Right, Top, Bottom) and use them in this FFmpeg command:

Bash
# If you found: Left=100, Top=50, Width=800, Height=600
ffmpeg -i input.mp4 -vf "crop=800:600:100:50" -c:a copy output_cropped.mp4

Important Tips:

  • Re-encoding: Because you are changing the actual picture, FFmpeg has to re-encode the video. I didn't include quality flags above to keep it simple, but for the best result, add -crf 20: ffmpeg -i input.mp4 -vf "crop=w:h:x:y" -c:v libx264 -crf 20 -c:a copy output.mp4

  • Even Numbers: Just like before, H.264 (MP4) loves even numbers. If you get an error, make sure your width and height are divisible by 2.


How to convert a .webm video into a .mp4 one?

ffmpeg -i output_trimmed.webm -c:v libx264 -crf 23 -preset medium -c:a aac output.mp4

How to trim a video on Ubuntu?

ffmpeg -i toAi.webm -ss 02:42 -to 05:59 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output_trimmed.webm

30 January 2026

How to move all the files in a directory to another directory on Linux

Gemini, I used the command -

$ ls -alhSp | grep -v /

, and got a list of files in the current directory.

What command can I use to move the files in the above list to another directory, please?


That is a very specific (and clever) command! You're using ls to list all files, sorting them by size (-S), adding a trailing slash to directories (-p), and then using grep to filter out anything with a slash.

Essentially, you've isolated just the regular files in your current folder, ranked from largest to smallest.

To move that specific list to another directory, you have a few options depending on how "clean" you want the process to be.


Method 1: The "Xargs" Approach (Recommended)

This is the most efficient way to take the output of one command and pipe it into another. We use the -I flag to create a placeholder (often {}) for the filenames.

Bash
ls -alhSp | grep -v / | awk '{print $9}' | xargs -I {} mv {} /path/to/destination/
  • awk '{print $9}': This is necessary because your original command uses -l (long format). awk grabs just the filename (the 9th column) so mv doesn't try to move the file permissions or timestamps.

  • xargs -I {}: This tells the system: "Take each filename and put it where the {} is in the next command."

Method 2: The Simple "Wildcard" (If you don't need the sort order)

If your goal is simply to move all files but no folders and you don't actually care about the size sorting from your original command, a simple loop is often safer:

Bash
for file in *; do [ -f "$file" ] && mv "$file" /path/to/destination/; done

⚠️ A Note on File Names

If any of your files have spaces in their names, the ls | grep method can get messy. When dealing with complex file movements, using find is generally the "Gold Standard" because it handles hidden characters and spaces much better:

Bash
find . -maxdepth 1 -type f -exec mv {} /path/to/destination/ \;
  • -maxdepth 1: Prevents it from looking into subfolders.

  • -type f: Ensures only files are moved (ignoring directories).


Which one should you use?

If you want...Use...
To keep using your ls logicMethod 1 (with awk)
The safest/fastest wayThe find command
To avoid complex syntaxMethod 2 (The loop)

Would you like me to explain how to modify that command to only move files over a certain size?


Yes, please. Thank you.

 

 

Since you are already using ls -S (sort by size), we can refine your pipeline to filter for specific size thresholds.

To do this effectively, we’ll swap ls for the find command. While ls is great for looking at files, find is a powerhouse for filtering them by attributes like size, age, or type.


Using find for Specific Sizes

This is the most reliable method. You can specify "greater than" (+) or "less than" (-) a certain size.

To move files larger than 100MB:

Bash
find . -maxdepth 1 -type f -size +100M -exec mv {} /path/to/destination/ \;

To move files smaller than 50KB:

Bash
find . -maxdepth 1 -type f -size -50k -exec mv {} /path/to/destination/ \;

Breakdown of the logic:

  • .: Look in the current directory.

  • -maxdepth 1: Don't go digging into subfolders.

  • -type f: Only look for files (this replaces your grep -v / logic).

  • -size +100M: The filter. Use k for Kilobytes, M for Megabytes, or G for Gigabytes.

  • -exec mv {} ...: The "action" part that moves every file found.


The "Quick & Dirty" Way (Using your original command)

If you prefer to stick to your ls command and just want to "eyeball" it, you can use head to move only the top largest files.

To move the top 5 largest files:

Bash
ls -pS | grep -v / | head -n 5 | xargs -I {} mv {} /path/to/destination/

Note: This version removes the -alh flags. When piping to mv, we don't want the "human-readable" sizes or permissions (like 1.2G or -rw-r--r--) included in the text, otherwise mv will get confused and think those are the filenames!


Comparison of Size Units

When using the -size flag in find, keep these suffixes in mind:

SuffixMeaningExample
kKilobytes-size +50k (Over 50 KB)
MMegabytes-size +10M (Over 10 MB)
GGigabytes-size +1G (Over 1 GB)
cBytes-size +100c (Over 100 Bytes)

Would you like me to show you how to do a "dry run" first, so you can see exactly which files will be moved before actually moving them?

 Yes, please. Thank you.

 

Performing a dry run is a pro move. It allows you to verify your logic without actually changing any files—saving you from that "oh no" moment when you accidentally move the wrong thing.

The simplest way to do a dry run is to replace the mv command with echo. This will print the command to your terminal instead of executing it.


1. Dry Run with the find Command

This is the safest way to test your size filters.

Bash
find . -maxdepth 1 -type f -size +100M -exec echo "Moving:" {} \;
  • What happens: The terminal will list every file larger than 100MB preceded by the word "Moving:". It won't actually move anything.

2. Dry Run with your ls Pipeline

If you are using the pipe method with xargs, just put echo before mv.

Bash
ls -pS | grep -v / | head -n 5 | xargs -I {} echo mv {} /path/to/destination/
  • What happens: You will see a list of commands like mv file1.zip /dest/. If the paths look correct, you can just hit the "Up" arrow on your keyboard and remove the echo to run it for real.


Comparison of Workflows

StepActionCommand Key
1. IdentifyList and filterfind or ls | grep
2. VerifyThe Dry Runecho
3. ExecuteFinal Movemv

A Quick Pro-Tip: The "Verbose" Flag

If you want to see the files moving while they are actually being moved (a middle ground between a dry run and flying blind), use the -v flag with mv:

Bash
mv -v source_file destination/
# Output: 'source_file' -> 'destination/source_file'

Would you like to know how to undo a move if you realize you sent files to the wrong folder?

 Yes, please. Thank you.

 

Moving files is instantaneous on the same disk, so there isn't a "Ctrl+Z" or "Undo" button in the terminal. However, you can effectively "reverse" the move by flipping the logic of your command.

Here are the best ways to handle an accidental move:

1. The "Reverse Move" (The Manual Fix)

If you just moved a specific group of files into a folder, the quickest way back is to go into that folder and move them back to the parent directory (represented by ..).

Bash
cd /path/to/destination/
mv * ../
  • Warning: Only do this if the destination folder was empty before you moved your files into it. Otherwise, you'll end up moving the original contents of that folder back to your source directory too!


2. The "By Time" Undo (The Precision Fix)

If you moved files into a folder that already had other files in it, you can use find to identify only the files that arrived in the last few minutes.

To move files out that arrived in the last 5 minutes:

Bash
find /path/to/destination/ -maxdepth 1 -type f -cmin -5 -exec mv {} ./ \;
  • -cmin -5: Finds files whose status changed (moved) less than 5 minutes ago.

  • ./: Moves them back to your current working directory.


3. Preventing Accidents: The "Interactive" Flag

To prevent needing an "undo" in the future, you can use the -i (interactive) flag. This forces the terminal to ask for your permission before overwriting any files at the destination.

Bash
mv -i source_file destination/
# Terminal: overwrite destination/source_file? (y/n)

Best Practices Summary

SituationSafety Strategy
Before movingUse echo for a Dry Run.
During the moveUse mv -v (Verbose) to see what's happening.
Potential OverwritesUse mv -n (No-clobber) to prevent overwriting existing files.
The "Undo"Use find with -cmin to isolate recently moved files.

Would you like me to show you how to create an "alias" so you can run these complex size-filtering moves with a single short command in the future?

 

 

 

 

25 December 2025

How to compare differences of files in Vim?

In Vim, you can compare (diff) the file you already have open with another file in a few common ways. Here are the most practical ones.


1️⃣ From inside Vim (recommended)

If you already have file A open and want to compare it with file B:

:vert diffsplit path/to/other_file
  • Opens the other file in a vertical split

  • Vim automatically enters diff mode

  • Differences are highlighted

Horizontal split (if you prefer):

:diffsplit path/to/other_file

2️⃣ If both files are already open in Vim

If you opened two files normally (no diff yet):

:diffthis

Run this command in each window you want to compare.

To turn diff mode off later:

:diffoff

3️⃣ Start Vim in diff mode (for future reference)

From the terminal:

vim -d file1 file2

or

vimdiff file1 file2

4️⃣ Useful diff navigation commands

While in diff mode:

CommandWhat it does
]cJump to next difference
[cJump to previous difference
:diffgetGet change from the other file
:diffputPut change to the other file
dodiff obtain (same as diffget)
dpdiff put

5️⃣ Exit diff mode cleanly

:diffoff!

or close one of the windows:

:q

💡 Tip (since you use Linux a lot)

Vertical diff is often easier on wide terminals:

:vert diffsplit other_file

If you want, I can also show:

  • how to compare 3 files

  • how to diff against the current buffer on disk

  • how to use Vim diff with Git

Just tell me 👍

How to compare 3 files, please.

 

 

In Vim, comparing 3 files is very doable. Here are the clean, correct ways, from simplest to more advanced.


✅ Method 1: Start Vim with 3 files in diff mode (best & simplest)

From the terminal:

vimdiff file1 file2 file3

or

vim -d file1 file2 file3

Result:

  • All 3 files open side-by-side

  • Vim automatically enters diff mode

  • Differences highlighted across all three


✅ Method 2: Add a 3rd file while already diffing 2 files

If you already have file1 open and diffed with file2:

:vert diffsplit file3

Vim will:

  • Open file3 in a new split

  • Automatically include it in the diff

💡 For horizontal split instead:

:diffsplit file3

✅ Method 3: Files already open → manually enable diff

If all three files are open in different windows but not diffed yet:

  1. Go to each window and run:

    :diffthis
    
  2. Repeat for all three windows

To exit later:

:diffoff

🧭 Navigating differences (very important with 3 files)

CommandDescription
]c / [cNext / previous diff
doTake change from another file
dpPut change into another file
:diffget LOCALGet change from current window
:diffget REMOTEGet change from another window

👉 With 3 files, Vim labels them as:

  • LOCAL

  • BASE

  • REMOTE

You can be explicit:

:diffget BASE
:diffget LOCAL
:diffget REMOTE

🧠 Common real-world example (Git-style 3-way diff)

vimdiff mine theirs base

Typical meaning:

  • BASE = original

  • LOCAL = your version

  • REMOTE = incoming version


🧹 Exit diff mode

:diffoff!

or close windows normally:

:q

⭐ Pro tips

  • Use vertical splits on wide screens:

    set diffopt+=vertical
    
  • Synchronize scrolling:

    :set scrollbind
    

If you want, I can also explain:

  • 3-way merge conflict resolution

  • diff against Git commits

  • color customization for diffs

Just say the word 👍

14 December 2025

On Ubuntu, how to run one of the commands I ran before again?

To rerun a command from your history, use ! followed by its number (e.g., !123), or use !! for the very last command, !-2 for the second-to-last, or press Ctrl+r and type part of the command to search for it, then hit Enter. 
By History Number
  1. View History: Type history and press Enter to see a numbered list.
  2. Re-run: Type ! then the number of the command (e.g., !567 to run command #567). 
bash
# Example:
history
# ... (list of commands with numbers) ...
!123 # Runs the 123rd command
Shortcuts
  • !!: Runs the immediately preceding command.
  • !-2: Runs the second-to-last command.
  • !string: Runs the most recent command starting with string (e.g., !ls runs the last ls command). 
Reverse Search (Ctrl+r) 
  • Press Ctrl+r to enter interactive search mode.
  • Type part of the command you're looking for (e.g., ssh).
  • Press Ctrl+r again to cycle through older matches.
  • Press Enter to run the command, or Esc to exit search without running. 
  • If you have cycled through some older matches, now you want to cycle
    through some new 
    matches, you can add the following line to your  ~/.bashrc file:
stty -ixon
Starting now, you can press   Ctrl+s to cycle through new matches.