Overview of Unix Commands for C Development

TL;DR
A quick reference of Unix commands for managing C projects: sed to convert leading spaces to tabs, dos2unix to fix Windows line endings, style checkers to enforce coding standards, and gcc and gdb to compile with warnings and debug.
On this page
This document provides an explanation of various Unix commands used in the management and development of C software projects, highlighting their purposes and usage contexts.
File Formatting and Conversion
Convert spaces to tabs in C and header files:
sed -i 's/^[ ]\{4\}/\t/' *.c *.h
^[ \t]+
This command uses ‘sed’ to replace the first four spaces at the beginning of each line with a tab in all .c and .h files, modifying files in-place.
Convert DOS to Unix line endings in C and header files:
dos2unix *.c *.h
Applies the ‘dos2unix’ utility to convert line endings from CRLF (Carriage Return + Line Feed) to LF (Line Feed), which is standard in Unix/Linux environments.
Apply global DOS to Unix conversion and space-to-tab replacement:
find . -type f -exec dos2unix {} +
find . -type f -exec sed -i 's/^[ ]\{4\}/\t/' {} +
Runs ‘dos2unix’ and ‘sed’ on every file in the directory and subdirectories, converting line endings and replacing leading spaces with tabs, respectively.
Exclude .git directory during formatting:
find . -path './.git' -prune -o -type f -exec sed -i 's/^[ ]\{4\}/\t/' {} +
find . -path './.git' -prune -o -type f -exec dos2unix {} +
Specifically avoids modifying files within the ‘.git’ directory while performing the same operations as above.
Code Style Enforcement
Check coding style of all C and header files:
betty *.c *.h
Applies ‘Betty’ style checks to all .c and .h files in the directory, enforcing consistency and standards across the project.
Compilation and Debugging
Compile all C files into an executable:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o shell
Compiles all .c files with strict compiler warnings and standards enforced, creating an executable named ‘shell’.
Memory leak check with Valgrind:
valgrind --leak-check=full ./shell
Runs the compiled ‘shell’ executable through ‘Valgrind’ with a full leak check enabled, identifying any memory management issues.
Key takeaways 5
- sed can convert leading spaces to tabs across .c and .h files.
- dos2unix converts CRLF line endings to Unix LF.
- Style checkers keep a C codebase consistent.
- Compile with gcc using strict warning flags.
- Use gdb and valgrind to debug and find memory errors.
Watch & learn
Frequently asked questions
How do I convert Windows line endings to Unix?
Run dos2unix on the files, for example dos2unix *.c *.h, which converts CRLF (\r\n) endings to LF (\n).
What gcc flags should I use for C projects?
Common strict flags are -Wall -Werror -Wextra -pedantic, often with a standard such as -std=gnu89 or -std=c99, to catch mistakes early.
How do I debug a C program on Unix?
Compile with -g to include debug symbols, then run it in gdb to set breakpoints and inspect variables, and use valgrind to detect memory leaks.
Learn it hands-on at AL Academy
Free practical programs in software, automation and IT, with labs and certificates.
Related articles
Basic Unix Commands | 02x00.06-01
To navigate through the file system in Unix, you use the cd (change directory) command. Here are some examples:

Advanced Ubuntu Terminal Commands | 02x01.06-01
Ten advanced Ubuntu terminal commands every power user should know, from grep, awk and sed to rsync, tmux, cron, ssh, find and docker.

Introduction to Emacs | 02x00.03-02
Emacs is not just a text editor; it is a powerful tool that can revolutionize the way you interact with your computer.

Comments
No comments yet. Start the conversation.