Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre One-line Automation Scripts | Automation and Troubleshooting
Linux Terminal Tools

bookOne-line Automation Scripts

Using one-liner scripts in the Linux terminal lets you automate repetitive or complex tasks with a single command. These compact scripts can save you time, reduce manual errors, and help you streamline your workflow. By combining multiple commands and logic into a single line, you can quickly perform actions like batch renaming files, extracting information, or processing data, all without writing full scripts or leaving your shell session.

# Batch rename all .txt files by adding "_old" before the extension
for file in *.txt; do mv "$file" "${file%.txt}_old.txt"; done
Note
Note

Shell variable expansion and quoting are crucial for writing reliable one-liners. In the previous example, "${file%.txt}_old.txt" uses parameter expansion to remove the .txt extension and append _old.txt. Quoting variables (like "$file") prevents issues with filenames containing spaces or special characters.

To understand how the batch rename one-liner works, break it down step by step. The for file in *.txt; part loops over every file in the current directory ending with .txt. The do and done keywords define the loop's body. Inside the loop, mv "$file" "${file%.txt}_old.txt" renames each file. Here, $file holds the current filename, and ${file%.txt}_old.txt uses shell variable expansion to strip the .txt extension from the filename and add _old.txt instead. Quoting each variable ensures the command works correctly even if filenames contain spaces.

# Extract all lines containing "error" from log files and save to summary.txt
grep -i "error" *.log > summary.txt

When writing one-liners for daily CLI work, always quote variables to handle spaces and special characters safely. Test your one-liners on a small set of files before running them on important data. Use dry-run options (like adding echo before commands) to preview actions. Save frequently used one-liners as shell aliases or scripts for reuse. Clear, concise one-liners can become powerful tools in your automation toolkit, but always double-check commands to avoid accidental data loss.

question mark

Which of the following is a best practice when writing one-liner scripts in the terminal?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookOne-line Automation Scripts

Glissez pour afficher le menu

Using one-liner scripts in the Linux terminal lets you automate repetitive or complex tasks with a single command. These compact scripts can save you time, reduce manual errors, and help you streamline your workflow. By combining multiple commands and logic into a single line, you can quickly perform actions like batch renaming files, extracting information, or processing data, all without writing full scripts or leaving your shell session.

# Batch rename all .txt files by adding "_old" before the extension
for file in *.txt; do mv "$file" "${file%.txt}_old.txt"; done
Note
Note

Shell variable expansion and quoting are crucial for writing reliable one-liners. In the previous example, "${file%.txt}_old.txt" uses parameter expansion to remove the .txt extension and append _old.txt. Quoting variables (like "$file") prevents issues with filenames containing spaces or special characters.

To understand how the batch rename one-liner works, break it down step by step. The for file in *.txt; part loops over every file in the current directory ending with .txt. The do and done keywords define the loop's body. Inside the loop, mv "$file" "${file%.txt}_old.txt" renames each file. Here, $file holds the current filename, and ${file%.txt}_old.txt uses shell variable expansion to strip the .txt extension from the filename and add _old.txt instead. Quoting each variable ensures the command works correctly even if filenames contain spaces.

# Extract all lines containing "error" from log files and save to summary.txt
grep -i "error" *.log > summary.txt

When writing one-liners for daily CLI work, always quote variables to handle spaces and special characters safely. Test your one-liners on a small set of files before running them on important data. Use dry-run options (like adding echo before commands) to preview actions. Save frequently used one-liners as shell aliases or scripts for reuse. Clear, concise one-liners can become powerful tools in your automation toolkit, but always double-check commands to avoid accidental data loss.

question mark

Which of the following is a best practice when writing one-liner scripts in the terminal?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
some-alt